零、前言
[1].两个Fragment,点击左右按钮切换相应的Fragment
[2].观察Activity 和两个Fragment生命周期变化
[3].布局很简单,挺多的,就不贴了,会给图
具体情况如图:
布局:
LifeOfFgActivity
public class LifeOfFgActivity extends AppCompatActivity {
private static final String TAG = "LifeOfFgActivity";
@BindView(R.id.btn_left)
Button mTvTitle1;
@BindView(R.id.btn_right)
Button mTvTitle2;
@BindView(R.id.ll_life_root)
LinearLayout mLlLifeRoot;
@BindView(R.id.fl_life_root)
FrameLayout mFlLifeRoot;
private FragmentManager mFm;
private FragmentTransaction mFt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_life);
Log.e(TAG, "onCreate: &&&&&&LifeOfFgActivity");
ButterKnife.bind(this);
initFragment();//初始Fragment
}
@Override
protected void onStart() {
super.onStart();
Log.e(TAG, "onStart: &&&&&&LifeOfFgActivity");
}
@Override
protected void onResume() {
super.onResume();
Log.e(TAG, "onResume: &&&&&&LifeOfFgActivity");
}
@Override
protected void onPause() {
super.onPause();
Log.e(TAG, "onPause: &&&&&&LifeOfFgActivity");
}
@Override
protected void onStop() {
super.onStop();
Log.e(TAG, "onStop: &&&&&&LifeOfFgActivity");
}
@Override
protected void onRestart() {
super.onRestart();
Log.e(TAG, "onRestart: &&&&&&LifeOfFgActivity");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.e(TAG, "onDestroy: &&&&&&LifeOfFgActivity");
}
private void initFragment() {
mFm = getFragmentManager();//1.获取FragmentManager
mFt = mFm.beginTransaction();//2.fm开启事务
mFt.add(R.id.fl_life_root, new Fragment1()); //3.动态添加
mFt.commit();//4.提交事务
}
@OnClick({R.id.btn_left, R.id.btn_right})
public void onViewClicked(View view) {
mFt = mFm.beginTransaction();//2.fm开启事务
switch (view.getId()) {
case R.id.btn_left:
mFlLifeRoot.removeAllViews();//先清屏
mFt.replace(R.id.fl_life_root, new Fragment1()); //3.动态添加
break;
case R.id.btn_right:
mFlLifeRoot.removeAllViews();//先清屏
mFt.replace(R.id.fl_life_root, new Fragment2()); //3.动态添加
break;
}
mFt.commit();//4.提交事务
}
}
Fragment1
public class Fragment1 extends Fragment {
private static final String TAG = "Fragment1";
@Override
public void onAttach(Context context) {
super.onAttach(context);
Log.e(TAG, "onAttach: ====Fragment1");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e(TAG, "onCreate: ====Fragment1");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.e(TAG, "onCreateView:====Fragment1 ");
return inflater.inflate(R.layout.fragment_fragment1, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.e(TAG, "onActivityCreated:====Fragment1 ");
}
@Override
public void onStart() {
super.onStart();
Log.e(TAG, "onStart: ====Fragment1");
}
@Override
public void onResume() {
super.onResume();
Log.e(TAG, "onResume:====Fragment1 ");
}
@Override
public void onPause() {
super.onPause();
Log.e(TAG, "onPause:====Fragment1 ");
}
@Override
public void onStop() {
super.onStop();
Log.e(TAG, "onStop: ====Fragment1");
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.e(TAG, "onDestroyView: ====Fragment1");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e(TAG, "onDestroy:====Fragment1 ");
}
@Override
public void onDetach() {
super.onDetach();
Log.e(TAG, "onDetach:====Fragment1 ");
}
}
Fragment2
public class Fragment2 extends Fragment {
private static final String TAG = "Fragment2";
@Override
public void onAttach(Context context) {
super.onAttach(context);
Log.e(TAG, "onAttach: >>>>>Fragment2");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e(TAG, "onCreate: >>>>>Fragment2");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.e(TAG, "onCreateView:>>>>>Fragment2 ");
return inflater.inflate(R.layout.fragment_fragment2, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.e(TAG, "onActivityCreated:>>>>>Fragment2 ");
}
@Override
public void onStart() {
super.onStart();
Log.e(TAG, "onStart: >>>>>Fragment2");
}
@Override
public void onResume() {
super.onResume();
Log.e(TAG, "onResume:>>>>>Fragment2 ");
}
@Override
public void onPause() {
super.onPause();
Log.e(TAG, "onPause:>>>>>Fragment2 ");
}
@Override
public void onStop() {
super.onStop();
Log.e(TAG, "onStop: >>>>>Fragment2");
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.e(TAG, "onDestroyView: >>>>>Fragment2");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e(TAG, "onDestroy:>>>>>Fragment2 ");
}
@Override
public void onDetach() {
super.onDetach();
Log.e(TAG, "onDetach:>>>>>Fragment2 ");
}
}
后记、
1.声明:
[1]本文由张风捷特烈原创,转载请注明
[2]欢迎广大编程爱好者共同交流
[3]个人能力有限,如有不正之处欢迎大家批评指证,必定虚心改正
[4]你的喜欢与支持将是我最大的动力
2.连接传送门:
更多安卓技术欢迎访问:安卓技术栈
我的github地址:欢迎star
简书首发,腾讯云+社区同步更新
张风捷特烈个人网站,编程笔记请访问:http://www.toly1994.com
3.联系我
QQ:1981462002
邮箱:1981462002@qq.com
微信:zdl1994328