Android Fragment的使用 二 事务函数详解
2017-04-16 12:58
489 查看
我先把需要将的函数摆列出来
FragmentTransaction transaction = fm.benginTransatcion();//开启一个事务
transaction.add()
往Activity中添加一个Fragment
transaction.remove()
从Activity中移除一个Fragment,如果被移除的Fragment没有添加到回退栈(回退栈后面会详细说),这个Fragment实例将会被销毁。
transaction.replace()
使用另一个Fragment替换当前的,实际上就是remove()然后add()的合体~
transaction.hide()
隐藏当前的Fragment,仅仅是设为不可见,并不会销毁
transaction.show()
显示之前隐藏的Fragment
detach()
会将view从UI中移除,和remove()不同,此时fragment的状态依然由FragmentManager维护。
attach()
重建view视图,附加到UI上并显示。
transatcion.commit()//提交一个事务
使用例子如下:
我们在三个Fragment的输入栏都写入了数据,当我们从FragmentThree按下back键,回退到FragmentTwo,发现这输入栏还存在之前的数据,但再按下back键,FragmentOne的输入栏没有数据。
这是因为FragmentOne的replace将之前FragmentOne销毁了,然后添加了FragmenTwo,但在FragmentTwo使用了hide将FragmentTwo隐藏,将FragmentThree添加下来。
下期将Fragment与Activity的交互。
再见
FragmentTransaction transaction = fm.benginTransatcion();//开启一个事务
transaction.add()
往Activity中添加一个Fragment
transaction.remove()
从Activity中移除一个Fragment,如果被移除的Fragment没有添加到回退栈(回退栈后面会详细说),这个Fragment实例将会被销毁。
transaction.replace()
使用另一个Fragment替换当前的,实际上就是remove()然后add()的合体~
transaction.hide()
隐藏当前的Fragment,仅仅是设为不可见,并不会销毁
transaction.show()
显示之前隐藏的Fragment
detach()
会将view从UI中移除,和remove()不同,此时fragment的状态依然由FragmentManager维护。
attach()
重建view视图,附加到UI上并显示。
transatcion.commit()//提交一个事务
使用例子如下:
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentManager fm = getFragmentManager(); FragmentTransaction tx = fm.beginTransaction(); tx.add(R.id.id_content,new FragmentOne(),"ONE"); tx.commit(); } } public class FragmentOne extends Fragment implements View.OnClickListener { private Button button; @Override public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState){ View view = inflater.inflate(R.layout.fragment_one,container,false); button = (Button) view.findViewById(R.id.id_fragment_one_btn); button.setOnClickListener(this); return view; } @Override public void onClick(View v){ FragmentTwo fTwo = new FragmentTwo(); FragmentManager fm = getFragmentManager(); FragmentTransaction tx = fm.beginTransaction(); tx.replace(R.id.id_content,fTwo,"TWO"); tx.addToBackStack(null); tx.commit(); } } public class FragmentTwo extends Fragment implements View.OnClickListener { private Button button; @Override public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState){ View view = inflater.inflate(R.layout.fragment_two,container,false); button = (Button) view.findViewById(R.id.id_fragment_two_btn); button.setOnClickListener(this); return view; } @Override public void onClick(View v){ FragmentThree fThree = new FragmentThree(); FragmentManager fm = getFragmentManager(); FragmentTransaction tx = fm.beginTransaction(); tx.hide(this); tx.add(R.id.id_content,fThree,"THREE"); tx.addToBackStack(null); tx.commit(); } } public class FragmentThree extends Fragment implements View.OnClickListener { private Button button; @Override public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState){ View view = inflater.inflate(R.layout.fragment_three,container,false); button = (Button) view.findViewById(R.id.id_fragment_three_btn); button.setOnClickListener(this); return view; } @Override public void onClick(View v){ Toast.makeText(getActivity(),"three",Toast.LENGTH_SHORT).show(); } } activity_main <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 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="android.com.myapplication.MainActivity"> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/id_content"></FrameLayout> </RelativeLayout> fragment_one <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="one" android:id="@+id/id_fragment_one_btn" android:layout_gravity="center"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> fragment_two <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="two" android:id="@+id/id_fragment_two_btn" android:layout_gravity="center"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> fragment_three <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="three" android:id="@+id/id_fragment_three_btn" android:layout_gravity="center"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
我们在三个Fragment的输入栏都写入了数据,当我们从FragmentThree按下back键,回退到FragmentTwo,发现这输入栏还存在之前的数据,但再按下back键,FragmentOne的输入栏没有数据。
这是因为FragmentOne的replace将之前FragmentOne销毁了,然后添加了FragmenTwo,但在FragmentTwo使用了hide将FragmentTwo隐藏,将FragmentThree添加下来。
下期将Fragment与Activity的交互。
再见
相关文章推荐
- Android开发教程之Fragment定义、创建与使用方法详解【包含Activity通讯,事务执行等】
- Android Fragment使用详解
- Android fragment使用详解及案例
- Android fragment使用详解及案例
- android Fragment静态加载使用详解
- Android fragment使用详解及案例
- Android Fragment使用详解
- 【Android界面实现】FragmentPagerAdapter与FragmentStatePagerAdapter使用详解与区别
- Android Fragment详解,动态添加+静态使用+图+代码
- [转][Android]FragmentPagerAdapter与FragmentStatePagerAdapter使用详解与区别
- Android中Fragment的解析和使用详解
- Android Fragment 使用详解
- Android中Google地图路径导航,使用mapfragment地图上画出线路(google map api v2)详解
- Android Fragment使用详解及生命周期
- Android Fragment使用详解
- Android中的Fragment使用详解之页面加载
- Android FragmentTabHost 使用方法详解
- Android中的Fragment使用详解之生命周期
- android开发之Fragment 的生命周期及使用方法详解
- Android Fragment使用详解