转载自https://blog.csdn.net/u014699958/article/details/52996143
项目有一个Activity类和3个Fragment类。Activity 中放置3个按钮(实质TextView)可以切换到不同的Fragment上。下面将给出Activity 和Fragment的代码和layout。
1-1 Fragment1
public class Fragment1 extends Fragment {
final String TAG = " test Fragment1";
public Fragment1() {
// Required empty public constructor
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
Log.d(TAG,"onAttach");
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG,"onCreate");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1_layout, null);
Log.d(TAG,"onCreateView");
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d(TAG,"onActivityCreated");
}
@Override
public void onStart() {
super.onStart();
Log.d(TAG,"onStart");
}
@Override
public void onResume() {
super.onResume();
Log.d(TAG,"onResume");
}
@Override
public void onPause() {
super.onPause();
Log.d(TAG,"onPause");
}
@Override
public void onStop() {
super.onStop();
Log.d(TAG,"onStop");
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.d(TAG,"onDestroyView");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG,"onDestroy");
}
@Override
public void onDetach() {
super.onDetach();
Log.d(TAG,"onDetach");
}
}
1-2 fragment1_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#888"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="Fragment1"/>
</LinearLayout>
2-1 Fragment2
public class Fragment2 extends Fragment {
final String TAG = " test Fragment2";
public Fragment2() {
// Required empty public constructor
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
Log.d(TAG,"onAttach");
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG,"onCreate");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment2_layout, null);
Log.d(TAG,"onCreateView");
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d(TAG,"onActivityCreated");
}
@Override
public void onStart() {
super.onStart();
Log.d(TAG,"onStart");
}
@Override
public void onResume() {
super.onResume();
Log.d(TAG,"onResume");
}
@Override
public void onPause() {
super.onPause();
Log.d(TAG,"onPause");
}
@Override
public void onStop() {
super.onStop();
Log.d(TAG,"onStop");
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.d(TAG,"onDestroyView");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG,"onDestroy");
}
@Override
public void onDetach() {
super.onDetach();
Log.d(TAG,"onDetach");
}
}
2-2 fragment2_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#888"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="Fragment2"/>
</LinearLayout>
3-1 Fragment3
public class Fragment3 extends Fragment {
final String TAG = " test Fragment3";
public Fragment3() {
// Required empty public constructor
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
Log.d(TAG,"onAttach");
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG,"onCreate");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment3_layout, null);
Log.d(TAG,"onCreateView");
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d(TAG,"onActivityCreated");
}
@Override
public void onStart() {
super.onStart();
Log.d(TAG,"onStart");
}
@Override
public void onResume() {
super.onResume();
Log.d(TAG,"onResume");
}
@Override
public void onPause() {
super.onPause();
Log.d(TAG,"onPause");
}
@Override
public void onStop() {
super.onStop();
Log.d(TAG,"onStop");
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.d(TAG,"onDestroyView");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG,"onDestroy");
}
@Override
public void onDetach() {
super.onDetach();
Log.d(TAG,"onDetach");
}
}
3-2 fragment3_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#888"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="Fragment3"/>
</LinearLayout>
4-1 MainActivity
public class MainActivity extends Activity{
final String TAG = " test MainActivity";
TextView mFragment1Tv ;
TextView mFragment2Tv ;
TextView mFragment3Tv ;
FrameLayout mainll ;
FragmentManager manager ;
FragmentTransaction transaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
initView();
initListener();
initData();
Log.d(TAG,"onCreate");
}
private void initListener() {
mFragment1Tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Fragment1 fragment1= new Fragment1();
transaction = manager.beginTransaction();
transaction.add(R.id.main_ll, fragment1,"1");
transaction.commit();
}
});
mFragment2Tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Fragment2 fragment2= new Fragment2();
transaction = manager.beginTransaction();
transaction.add(R.id.main_ll, fragment2,"2");
transaction.commit();
}
});
mFragment3Tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Fragment3 fragment3= new Fragment3();
transaction = manager.beginTransaction();
transaction.add(R.id.main_ll, fragment3,"3");
transaction.commit();
}
});
}
private void initData() {
manager = getFragmentManager();
}
private void initView() {
mFragment1Tv = (TextView) findViewById(R.id.fragment1_tv);
mFragment2Tv = (TextView) findViewById(R.id.fragment2_tv);
mFragment3Tv = (TextView) findViewById(R.id.fragment3_tv);
mainll = (FrameLayout) findViewById(R.id.main_ll);
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG,"onCreate");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG,"onRestart");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG,"onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG,"onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG,"onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG,"onDestroy");
}
}
4-2 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/index"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<TextView
android:id="@+id/fragment1_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#bbb"
android:text="Fragment1"
android:textSize="22sp" />
<TextView
android:id="@+id/fragment2_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#bbb"
android:text="Fragment2"
android:textSize="22sp" />
<TextView
android:id="@+id/fragment3_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#bbb"
android:text="Fragment3"
android:textSize="22sp" />
</LinearLayout>
<View
android:layout_below="@id/index"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#aaa"/>
<LinearLayout
android:id="@+id/main_ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true">
</LinearLayout>
</RelativeLayout>
5 测试生命周期
(一) add 方法分析
1.启动app,log如图:
界面如下:
2.点击Fragment1,log如下:
界面如下:
3点击fragment2 log如下: (这里并没有触发fragment1的生命周期)
界面如下:
4点击fragment3 log如下:
界面如下:
5 在此时分为两次不同的操作:
5-1 测试返回
操作:点击返回键,此时应用已经退出。log如下:
5-2 home键测试
操作:(1)点击home键,app退到后台,log如下:
(2)点击应用图标重新打开app,log如下:
6 回退栈测试
再回到5-1 ,如果想要点击返回键回到上一个Fragment的话,那么就需要加入到会退栈。
修改 MainActivity 的方法initListener如下:
private void initListener() {
mFragment1Tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Fragment1 fragment1= new Fragment1();
transaction = manager.beginTransaction();
transaction.add(R.id.main_ll, fragment1,"1");
transaction.addToBackStack(null);
transaction.commit();
}
});
mFragment2Tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Fragment2 fragment2= new Fragment2();
transaction = manager.beginTransaction();
transaction.add(R.id.main_ll, fragment2,"2");
transaction.addToBackStack(null);
transaction.commit();
}
});
mFragment3Tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Fragment3 fragment3= new Fragment3();
transaction = manager.beginTransaction();
transaction.add(R.id.main_ll, fragment3,"3");
transaction.addToBackStack(null);
transaction.commit();
}
});
}
操作:执行 1.打开app,2.点击Fragment,3.点击Fragment2,4.点击Fragment3。
此时清空LogCat,再点击返回键Log如下:
此时界面回到Fragment2,而其他Fragment和Activity的生命周期是没有变化的。若再点击返回键将回到Fragment1界面,再点击返回键就是回到Activity界面了。
(二) replace方法分析
修改 MainActivity 的方法initListener如下:(主要是替换add为replace)
private void initListener() {
mFragment1Tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Fragment1 fragment1= new Fragment1();
transaction = manager.beginTransaction();
transaction.replace(R.id.main_ll, fragment1,"1");
//transaction.add(R.id.main_ll, fragment1,"1");
// transaction.addToBackStack(null);
transaction.commit();
}
});
mFragment2Tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Fragment2 fragment2= new Fragment2();
transaction = manager.beginTransaction();
transaction.replace(R.id.main_ll, fragment2,"2");
//transaction.add(R.id.main_ll, fragment2,"2");
// transaction.addToBackStack(null);
transaction.commit();
}
});
mFragment3Tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Fragment3 fragment3= new Fragment3();
transaction = manager.beginTransaction();
transaction.replace(R.id.main_ll, fragment3,"3");
//transaction.add(R.id.main_ll, fragment3,"3");
//transaction.addToBackStack(null);
transaction.commit();
}
});
}
1.测试replace
操作:启动app,然后点击Fragment1,再点击Fragment2,log如下:
2.测试返回键
在上一步骤的基础上点击物理返回键Log如下:(应用程序已经退出了)
3.在(二)1的基础上点击home键,再重新打开app,log如下:
4.此时看下会退栈的情况。
修改 MainActivity 的方法initListener,主要是取消
// transaction.addToBackStack(null);
1
的注释。
操作:打开app,点击Fragment1,点击Fragment2,清空logCat,再点击返回键,此时log如下:
可见replace 是会销毁掉被replace 的Fragment。
(三) hide &show方法分析。
修改 MainActivity如下:(在activity的oncreate中add Fragment1和Fragment2,修改mFragment2Tv的点击事件。)
public class MainActivity extends Activity{
final String TAG = "testFragment MainActivity";
TextView mFragment1Tv ;
TextView mFragment2Tv ;
TextView mFragment3Tv ;
FrameLayout mainll ;
FragmentManager manager ;
FragmentTransaction transaction;
Fragment1 fragment1;
Fragment2 fragment2;
Fragment3 fragment3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
initView();
initListener();
initData();
Log.d(TAG,"onCreate");
}
private void initListener() {
mFragment1Tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
transaction = manager.beginTransaction();
transaction.add(R.id.main_ll, fragment1,"1");
transaction.commit();
}
});
mFragment2Tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
transaction = manager.beginTransaction();
transaction.hide(fragment1).show(fragment2);
transaction.commit();
}
});
mFragment3Tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
transaction = manager.beginTransaction();
transaction.replace(R.id.main_ll, fragment3,"3");
// transaction.add(R.id.main_ll, fragment3,"3");
transaction.addToBackStack(null);
transaction.commit();
}
});
}
private void initData() {
manager = getFragmentManager();
fragment1= new Fragment1();
fragment2= new Fragment2();
fragment3= new Fragment3();
transaction = manager.beginTransaction();
transaction.add(R.id.main_ll, fragment2,"2");
transaction.add(R.id.main_ll, fragment1,"1");
transaction.commit();
}
private void initView() {
mFragment1Tv = (TextView) findViewById(R.id.fragment1_tv);
mFragment2Tv = (TextView) findViewById(R.id.fragment2_tv);
mFragment3Tv = (TextView) findViewById(R.id.fragment3_tv);
mainll = (FrameLayout) findViewById(R.id.main_ll);
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG,"onStart");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG,"onRestart");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG,"onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG,"onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG,"onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG,"onDestroy");
}
}
操作:启动app ,log如下图,代码里面是先add Fragment2 再add Fragment1,此时显示的界面是Fragment1 的界面,然后点击TextView mFragment2Tv,界面切换到了Fragment2上,而log无任何的信息打印,可见hide &show 方法不触发Fragment生命周期,只影响它的显示。
(四) remove方法分析
在 (三)hide&show方法分析 的代码基础上修改MainActivity 如下:
(其他代码不变,只修改为remove方法)
mFragment2Tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
transaction = manager.beginTransaction();
transaction.remove(fragment1).show(fragment2);
transaction.commit();
}
});
启动app 时的log:
此时点击Fragment2,log如下
:
———————————————
版权声明:本文为CSDN博主「hdcanta」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u014699958/article/details/52996143