接着上一篇
FragmentManager manager=getFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
transaction.add(R.id.main, first);
android:id="@+id/main"
Fragment first;
first =new FirstFragment();
transaction.commit();
为了给Activity提交改变事物,必须调用commit方法
到这里就可以正常的添加fragment了。
注意:每个事务(提交给Activity的每个变化称为一个事务)都需要一个取得一次fragmenttransaction对象
当然,我们的每次变化都可以用transaction.addToBackStack(null);方法保存到Activity的back stack,这是一个栈,可以返回你之前存入其中的状态
当fragment移除时,fragment会被销毁,而在commit()提交之前使用addToBackStack,他只会停止,如果导航回来,fragment就会恢复。
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.second.MainActivity" >
RelativeLayout>
firstfragment.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#FF0000" >
<TextView
android:layout_width="match_parent"
android:layout_height="146dp"
android:text="this is first fragment" />
FrameLayout>
secondfragment.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#FFFF11"
android:layout_marginTop="300dp"
>
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="this is second fragment" />
FrameLayout>
FirstFragment.java
public class FirstFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.firstfrgment,container,false);
}
}
SecondFragment.java
public class SecondFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.secondfragment,container,false);
}
}
MainActivity.java
public class MainActivity extends ActionBarActivity {
FirstFragment first;
SecondFragment second;
FragmentTransaction transaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
first =new FirstFragment();
second =new SecondFragment();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
FragmentManager manager=getFragmentManager();
transaction=manager.beginTransaction();
int id = item.getItemId();
if (id == R.id.add_first) {
transaction.add(R.id.main, first);
transaction.addToBackStack(null);
}
else if(id==R.id.add_second){
transaction.add(R.id.main, second);
transaction.addToBackStack(null);//Log下显示BackStack元素个数
}
else if(id==R.id.remove_first){
//transaction.replace(R.id.main, first);
transaction.remove(first);
transaction.addToBackStack(null);
}
else if(id==R.id.remove_second){
transaction.remove(second);
transaction.addToBackStack(null);
}
transaction.commit();
int TAG = getFragmentManager().getBackStackEntryCount();
Log.d("TAG",String.valueOf(TAG));
if(TAG>0)
Log.d("TAG",getFragmentManager().getBackStackEntryAt(TAG-1).toString());
return true;
}
}