FragmentActivity、FragmentManager、FragmentTransaction几个的关系
2017-03-08 19:35
357 查看
FragmentActivity提供这么一个方法
//
Return the FragmentManager for interacting with fragments associated with this activity.
public FragmentManager getSupportFragmentManager()
也就是说用于Activity与Fragment之间的联系交流
FragmentManager提供这么几个方法
可以通过定义一个 FragmentTransaction transaction = manager.beginTransaction();
也就是说可以通过id,或者tag的查找方法去找到对应的Fragment,例如下面用法
messageFragment = (MessageFragment)fragmentManager.findFragmentByTag("message");
callFragment = (CallFragment)fragmentManager.findFragmentByTag("call");
Tranction的使用
这个方法是将Fragment替换
详细解析如下
Schedules a commit of this transaction. The commit does not happen immediately; it will be scheduled as work on the main thread to be done the next time that thread is ready.
A transaction can only be committed with this method prior to its containing activity saving its state. If the commit is attempted after that point, an exception will be thrown. This is because the state after the commit can be lost if the activity needs to
be restored from its state. See
situations where it may be okay to lose the commit.
Returns
Returns the identifier of this transaction's back stack entry, if
been called. Otherwise, returns a negative number.
Like
allows the commit to be executed after an activity's state is saved. This is dangerous because the commit can be lost if the activity needs to later be restored from its state, so this should only be used for cases where it is okay for the UI state to change
unexpectedly on the user.
按我的理解相当于用于提交
主要常见的有这几种用法,具体实现查看E盘-AndroidProject-FragmentConvert
所以 完整地加载一个fragment需要如下
FragmentManager fragmentManager = getSupportFragmentManager();//获得Manager
FragmentTransaction transaction = fragmentManager.beginT
b743
ransaction();//获得事务
callFragment = new CallFragment();//创建一个自定义Fragment
transaction.add(R.id.fl_content, callFragment, "zhishi");//加进去事务
transaction.show(callFragment);//显示该fragment
transaction.hide(messageFragment);//隐藏另一个fragment
在Layout的代码如下
主视图
<FrameLayout
android:id="@+id/fl_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
另外还需要自己定义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:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="这是电话界面"/>
</LinearLayout>
自定义的CallFragment
public class CallFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_call, null);
}
}
主要常见的有这几种用法,具体实现查看E盘-AndroidProject-FragmentConvert
//
Return the FragmentManager for interacting with fragments associated with this activity.
public FragmentManager getSupportFragmentManager()
也就是说用于Activity与Fragment之间的联系交流
FragmentManager提供这么几个方法
abstract FragmentTransaction | beginTransaction() Start a series of edit operations on the Fragments associated with this FragmentManager. |
abstract Fragment | findFragmentById(int id) Finds a fragment that was identified by the given id either when inflated from XML or as the container ID when added in a transaction. |
abstract Fragment | findFragmentByTag(String tag) Finds a fragment that was identified by the given tag either when inflated from XML or as supplied when added in a transaction. |
messageFragment = (MessageFragment)fragmentManager.findFragmentByTag("message");
callFragment = (CallFragment)fragmentManager.findFragmentByTag("call");
Tranction的使用
abstract FragmentTransaction | replace(int containerViewId, Fragment fragment, String tag) Replace an existing fragment that was added to a container. |
abstract FragmentTransaction | replace(int containerViewId, Fragment fragment) Calls replace(int, Fragment, String)with a null tag. |
abstract int | commit() Schedules a commit of this transaction. |
abstract int | commitAllowingStateLoss() Like commit()but allows the commit to be executed after an activity's state is saved. |
public abstract int commit ()
Schedules a commit of this transaction. The commit does not happen immediately; it will be scheduled as work on the main thread to be done the next time that thread is ready.A transaction can only be committed with this method prior to its containing activity saving its state. If the commit is attempted after that point, an exception will be thrown. This is because the state after the commit can be lost if the activity needs to
be restored from its state. See
commitAllowingStateLoss()for
situations where it may be okay to lose the commit.
Returns
Returns the identifier of this transaction's back stack entry, if
addToBackStack(String)had
been called. Otherwise, returns a negative number.
public abstract int commitAllowingStateLoss ()
Like commit()but
allows the commit to be executed after an activity's state is saved. This is dangerous because the commit can be lost if the activity needs to later be restored from its state, so this should only be used for cases where it is okay for the UI state to change
unexpectedly on the user.
按我的理解相当于用于提交
主要常见的有这几种用法,具体实现查看E盘-AndroidProject-FragmentConvert
所以 完整地加载一个fragment需要如下
FragmentManager fragmentManager = getSupportFragmentManager();//获得Manager
FragmentTransaction transaction = fragmentManager.beginT
b743
ransaction();//获得事务
callFragment = new CallFragment();//创建一个自定义Fragment
transaction.add(R.id.fl_content, callFragment, "zhishi");//加进去事务
transaction.show(callFragment);//显示该fragment
transaction.hide(messageFragment);//隐藏另一个fragment
在Layout的代码如下
主视图
<FrameLayout
android:id="@+id/fl_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
另外还需要自己定义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:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="这是电话界面"/>
</LinearLayout>
自定义的CallFragment
public class CallFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_call, null);
}
}
主要常见的有这几种用法,具体实现查看E盘-AndroidProject-FragmentConvert
相关文章推荐
- Fragment有直接关系的关键性类FragmentManager,FragmentTransaction,FragmentActivity
- FragmentTransaction与Fragment生命周期的关系
- Viewpager和FragmentTransaction、Fragment生命周期的关系
- getFragmentManager().beginTransaction().replace有残留
- FragmentTransaction与Fragment生命周期的关系
- Fragment FragmentManager FragmentTransaction 详解
- FragmentTransaction与Fragment生命周期的关系
- FragmentTransaction与Fragment生命周期的关系
- Android 进阶17:Fragment FragmentManager FragmentTransaction 深入理解
- ServiceManager,SystemServer,SystemManagerService,SystemService—简述这个几个类的关系
- FragmentTransaction与Fragment生命周期的关系
- HibernateTransactionManager与HibernateTemplater是什么关系,其中一个是否多余?
- Fragment FragmentManager FragmentTransaction 详解
- getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit
- FragmentManager#popBackStack()或FragmentTransaction#replace(int, Fragment, String)出现重叠解决方案
- 安卓开发——问题:getFragmentManager().beginTransaction().add()传入v4.fragment出错
- 安卓FragmentManager中FragmentTransaction的介绍
- 几个性能调优视图的关系
- getFragmentManager动态添加、替换fragment
- Android——FragmentTransaction add 和 replace 区别...