Android初学第81天
Posted HankZhou
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android初学第81天相关的知识,希望对你有一定的参考价值。
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天的主要内容,如果未能解决你的问题,请参考以下文章