Fragment(2)构建一个灵活的UI,保存之前状态

来源:互联网 发布:多益网络下载 编辑:程序博客网 时间:2024/05/22 02:20

一个新闻软件,平板展示的是左边是标题,右边是新闻对应的内容。手机端就是第一个界面展示的是标题,点击进去就是新闻内容了。


 FragmentManager 类提供add,remove,replace等方法,我们可以在Activity运行时动态添加移除替换Fragment。

  在运行时添加一个Fragment到Activity中

  我们要用FragmentManager来创建一个FragmentTransaction,来增删替换Fragment。如果我们要在Activity中动态替换Fragment的话,应该在onCreate()中初始化Fragment。

一个很重要的原则,当我们要动态添加Fragment的时候,我们在layout中必须要有一个View容器,来存放将要插入的Fragment的layout

xml布局代码:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/fragment_container"    android:layout_width="match_parent"    android:layout_height="match_parent" />

在Activity中,使用支持包的话就是调用 getSupportFragmentManager() 获得一个FragmentManager。接着调用 beginTransaction() 方法创建一个FragmentTransaction 并且调用add()方法来添加一个Fragment。

当我们已经完成了这些添加之类的操作,一定要记得提交事务。 调用的是FragmentTransaction  的 commit()方法。

代码里面动态添加:

import android.os.Bundle;import android.support.v4.app.FragmentActivity;public class MainActivity extends FragmentActivity {    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.news_articles);        // Check that the activity is using the layout version with        // the fragment_container FrameLayout        if (findViewById(R.id.fragment_container) != null) {            // However, if we're being restored from a previous state,            // then we don't need to do anything and should return or else            // we could end up with overlapping fragments.            if (savedInstanceState != null) {                return;            }            // Create a new Fragment to be placed in the activity layout            HeadlinesFragment firstFragment = new HeadlinesFragment();                        // In case this activity was started with special instructions from an            // Intent, pass the Intent's extras to the fragment as arguments            firstFragment.setArguments(getIntent().getExtras());                        // Add the fragment to the 'fragment_container' FrameLayout            getSupportFragmentManager().beginTransaction()                    .add(R.id.fragment_container, firstFragment).commit();        }    }}
因为Fragment是在运行时添加到一个FrameLayout容器中,而不是在xml中用<fragment>元素插入的,所以,我么可以动态的移除或者是替换掉Fragment。

我们要随时记住的是当我们完成Fragment事务,比如替换或者是移除一个Fragment,它常常应适当的允许用户导航返回和撤销之前的事情。为了允许用户导航返回通过我们的Fragment事务,我们必须在提交 FragmentTransation 之前调用 addToBackStack() 。即为允许用户点击返回键的时候,返回之前的Fragment。


!!!记住!!!当我们移除或者是替换一个Fragment并且把它的事务添加到回退栈的时候,这个被移除的Fragment它就是处于stopped的状态中,而不是destroyed。如果用户返回恢复之前的Fragment,它是restart的。如果没有把它的事务添加到回退栈之中的话,那么Fragment在移除或者是替换的时候,就是destroy的了。添加事务最大的好处就是它还存在,能在回调方法中进行返回数据的更改,又能记住状态。这个和那个Activity保存之前的状态是一个意思的。


替换其他的Fragment

官网代码:

// Create fragment and give it an argument specifying the article it should showArticleFragment newFragment = new ArticleFragment();Bundle args = new Bundle();args.putInt(ArticleFragment.ARG_POSITION, position);newFragment.setArguments(args);FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();// Replace whatever is in the fragment_container view with this fragment,// and add the transaction to the back stack so the user can navigate backtransaction.replace(R.id.fragment_container, newFragment);transaction.addToBackStack(null);// Commit the transactiontransaction.commit();

addToBackStack()方法带有一个可选String参数,该String指定了一个唯一的事务名字。这个名字也不是必须除非我们想要完成之前的Fragment操作,可以使用FragmentManager.BackStackEntry来完成之前的Fragment操作


 


  


0 0