//事务在提交fragment对象的同时把fragment添加到 回退栈中
FragmentTransaction transaction = manager.beginTransaction(); transaction.add(R.id.real1,r1); transaction.addToBackStack(""); transaction.add(R.id.real1,list); transaction.commit(); FragmentTransaction transaction1 = manager.beginTransaction(); transaction1.add(R.id.real2,listfragment); transaction1.commit(); int backStackEntryCount = manager.getBackStackEntryCount();//栈内当前的容量 Log.e("###","当前容量"); manager.popBackStack(); int backStackEntryCount1 = manager.getBackStackEntryCount();//栈内移除后的容量 Log.e("###","移除后的容量");//xml文件布局
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".Main2Activity" android:id="@android:id/tabhost"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TabWidget android:layout_width="match_parent" android:layout_height="50dp" android:id="@android:id/tabs"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@android:id/tabcontent"> </FrameLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/layout01" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第一个"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/layout02" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第二个"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/layout03" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第三个"/> </LinearLayout> </TabWidget> </LinearLayout> </TabHost>//主页面文件布局
//创建一个类去继承TabActivity public class Main2Activity extends TabActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); //获得TabHost对象 TabHost tabHost = getTabHost(); //添加Tab tabHost.addTab(tabHost.newTabSpec("aaa") .setIndicator("新闻").setContent(R.id.layout01)); tabHost.addTab(tabHost.newTabSpec("bbb") .setIndicator("体育").setContent(R.id.layout01)); tabHost.addTab(tabHost.newTabSpec("ccc") .setIndicator("人文").setContent(R.id.layout01)); tabHost.getChildAt(0).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(Main2Activity.this,"点击了界面",Toast.LENGTH_SHORT).show(); } }); } }