标签:
我们都知道给一个activity动态添加fragment的时候 有下面几种添加方式
看一下布局文件
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 > 7 8 <FrameLayout 9 android:layout_width="wrap_content" 10 android:layout_height="100dp" 11 android:id="@+id/show_fragment" 12 ></FrameLayout> 13 14 </LinearLayout>
效果图:
一个帧布局动态显示Fragment对象,
1、
1 package com.xqx_backfragment; 2 3 import android.app.Activity; 4 import android.app.FragmentManager; 5 import android.app.FragmentTransaction; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.Button; 10 11 public class MainActivity extends Activity { 12 13 private Button btn_new; 14 private Button btn_back; 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 btn_new = (Button) findViewById(R.id.newfragment); 20 btn_back = (Button) findViewById(R.id.backfragment); 21 //新建一个新的fragment 22 btn_new.setOnClickListener(new OnClickListener() { 23 24 @Override 25 public void onClick(View v) { 26 // TODO Auto-generated method stub 27 //创建管理者对象 28 FragmentManager manager = getFragmentManager(); 29 //创建事务对象 30 FragmentTransaction action = manager.beginTransaction(); 31 MyFragment fragment = new MyFragment(); 32 action.add(R.id.show_fragment, fragment); 33 action.commit(); 34 35 // MyFragment fragment = new MyFragment(); 36 // action.add(R.id.show_fragment, fragment); 37 // action.addToBackStack(null); 38 // action.commit(); 39 } 40 }); 41 42 //返回 43 btn_back.setOnClickListener(new OnClickListener() { 44 45 @Override 46 public void onClick(View v) { 47 // TODO Auto-generated method stub 48 onBackPressed(); 49 } 50 }); 51 } 52 53 54 55 }
1 //创建管理者对象 2 FragmentManager manager = getFragmentManager(); 3 //创建事务对象 4 FragmentTransaction action = manager.beginTransaction(); 5 MyFragment fragment = new MyFragment(); 6 action.add(R.id.show_fragment, fragment); 7 action.commit();
每次点击新建按钮,都执行上面一段代码,也就是新建一个Fragment对象并加入到事务中,原来的fragment效果被覆盖,只显示最后加入事务的fragment对象,但是此时存在多个fragment对象,之前加入事务的fragment对象都一直存在
点击返回按钮销毁activity 退出activity
2、
1 package com.xqx_backfragment; 2 3 import android.app.Activity; 4 import android.app.FragmentManager; 5 import android.app.FragmentTransaction; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.Button; 10 11 public class MainActivity extends Activity { 12 13 private Button btn_new; 14 private Button btn_back; 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 btn_new = (Button) findViewById(R.id.newfragment); 20 btn_back = (Button) findViewById(R.id.backfragment); 21 //新建一个新的fragment 22 btn_new.setOnClickListener(new OnClickListener() { 23 24 @Override 25 public void onClick(View v) { 26 // TODO Auto-generated method stub 27 //创建管理者对象 28 FragmentManager manager = getFragmentManager(); 29 //创建事务对象 30 FragmentTransaction action = manager.beginTransaction(); 31 32 action.replace(R.id.show_fragment, fragment); 33 action.commit(); 34 35 } 36 }); 37 38 //返回 39 btn_back.setOnClickListener(new OnClickListener() { 40 41 @Override 42 public void onClick(View v) { 43 // TODO Auto-generated method stub 44 onBackPressed(); 45 } 46 }); 47 } 48 49 50 51 }
1 //创建管理者对象 2 FragmentManager manager = getFragmentManager(); 3 //创建事务对象 4 FragmentTransaction action = manager.beginTransaction(); 5 MyFragment fragment = new MyFragment(); 6 action.replace(R.id.show_fragment, fragment); 7 action.commit();
每次点击新建按钮,都执行上面一段代码,也就是新建一个Fragment对象并加入到事务中,并且替代原来存在的fragment对象,这种情况下R.id.show_fragment视图上始终只有一个fragment对象
点击返回按钮销毁activity 退出activity
3、
1 package com.xqx_backfragment; 2 3 import android.app.Activity; 4 import android.app.FragmentManager; 5 import android.app.FragmentTransaction; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.Button; 10 11 public class MainActivity extends Activity { 12 13 private Button btn_new; 14 private Button btn_back; 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 btn_new = (Button) findViewById(R.id.newfragment); 20 btn_back = (Button) findViewById(R.id.backfragment); 21 //新建一个新的fragment 22 btn_new.setOnClickListener(new OnClickListener() { 23 24 @Override 25 public void onClick(View v) { 26 // TODO Auto-generated method stub 27 //创建管理者对象 28 FragmentManager manager = getFragmentManager(); 29 //创建事务对象 30 FragmentTransaction action = manager.beginTransaction(); 31 32 MyFragment fragment = new MyFragment(); 33 action.add(R.id.show_fragment, fragment); 34 action.addToBackStack(null); 35 action.commit(); 36 37 } 38 }); 39 40 //返回 41 btn_back.setOnClickListener(new OnClickListener() { 42 43 @Override 44 public void onClick(View v) { 45 // TODO Auto-generated method stub 46 onBackPressed(); 47 } 48 }); 49 } 50 51 52 53 }
1 //创建管理者对象 2 FragmentManager manager = getFragmentManager(); 3 //创建事务对象 4 FragmentTransaction action = manager.beginTransaction(); 5 6 MyFragment fragment = new MyFragment(); 7 action.add(R.id.show_fragment, fragment); 8 action.addToBackStack(null); 9 action.commit();
每次点击新建按钮,都执行上面一段代码,也就是新建一个Fragment对象并加入到事务中,并将对象加入到回退栈中显示出新的fragment效果图
点击返回按钮显示当前fragment之前的fragment效果图 直到没有fragment对象时activity销毁退出
----------------------------------------------------------------------------------------------------------------------------------------
根据需求选择上述三种事务添加fragment对象的方法。
注意:
1、每次添加fragment对象都要将事务提交才有效果(action.commit()) ,下面情况没有事务提交 则fragment对象没有成功加入事务
1 //创建管理者对象 2 FragmentManager manager = getFragmentManager(); 3 //创建事务对象 4 FragmentTransaction action = manager.beginTransaction(); 5 MyFragment fragment = new MyFragment(); 6 action.add(R.id.show_fragment, fragment);
正确写法:
1 //创建管理者对象 2 FragmentManager manager = getFragmentManager(); 3 //创建事务对象 4 FragmentTransaction action = manager.beginTransaction(); 5 MyFragment fragment = new MyFragment(); 6 action.add(R.id.show_fragment, fragment);
7 action.commit();
2、每次事务提交之前都要有这个事务对象,下面情况action.commit()之后 不能继续action.commit()
1 //创建管理者对象 2 FragmentManager manager = getFragmentManager(); 3 //创建事务对象 4 FragmentTransaction action = manager.beginTransaction(); 5 MyFragment fragment = new MyFragment(); 6 action.add(R.id.show_fragment, fragment); 7 action.commit(); 8 MyFragment fragment1 = new MyFragment(); 9 action.add(R.id.show_fragment, fragment1); 10 action.commit();
正确写法:
1 //创建管理者对象 2 FragmentManager manager = getFragmentManager(); 3 //创建事务对象 4 FragmentTransaction action = manager.beginTransaction(); 5 MyFragment fragment = new MyFragment(); 6 action.add(R.id.show_fragment, fragment); 7 action.addToBackStack(null); 8 action.commit(); 9 10 action = manager.beginTransaction(); 11 MyFragment fragment1 = new MyFragment(); 12 action.add(R.id.show_fragment, fragment1); 13 action.addToBackStack(null); 14 action.commit();
安卓开发_浅谈Fragment之事务添加Fragment对象
标签:
原文地址:http://www.cnblogs.com/xqxacm/p/4797346.html