码迷,mamicode.com
首页 > 移动开发 > 详细

Android初学第81天

时间:2018-06-22 19:33:25      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:protect   sid   begin   pat   new   extend   save   tac   return   

Android初学第81天

20_MVVM

代码

BeatBox

BeatBoxActivity.java

package com.bignerdranch.android.beatbox;

import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class BeatBoxActivity extends SingleFragmentActivity {
    @Override
    protected Fragment createFragment() {
        return BeatBoxFragment.newInstance();
    }
}

SingleFragmentActivity.java

package com.bignerdranch.android.beatbox;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;

public abstract class SingleFragmentActivity extends AppCompatActivity {
    protected abstract Fragment createFragment();

    protected int getLayoutResId() {
        return R.layout.activity_fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResId());
        FragmentManager manager = getSupportFragmentManager();
        Fragment fragment = manager.findFragmentById(R.id.fragment_container);

        if (fragment == null) {
            fragment = createFragment();
            manager.beginTransaction()
                .add(R.id.fragment_container, fragment)
                .commit();
        }
    }
}

Android初学第81天

标签:protect   sid   begin   pat   new   extend   save   tac   return   

原文地址:https://www.cnblogs.com/zhouheng0918/p/9215160.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!