转:原文链接 http://www.cnblogs.com/mengdd/archive/2013/01/09/2853254.html

管理Fragments

FragmentManager

  为了管理Activity中的fragments,需要使用FragmentManager.

  为了得到它,需要调用Activity中的getFragmentManager()方法。

  因为FragmentManager的API是在Android 3.0,也即API level 11开始引入的,所以对于之前的版本,需要使用support library中的FragmentActivity,并且使用getSupportFragmentManager()方法。

用FragmentManager可以做的工作有:

  得到Activity中存在的fragment:

  使用findFragmentById()或findFragmentByTag()方法。

  将fragment弹出back stack:

  popBackStack():将back stack中最后一次的fragment转换弹出。如果没有可以出栈的东西,返回false。

  这个函数是异步的:它将弹出栈的请求加入队列,但是这个动作直到应用回到事件循环才会执行。

  为back stack加上监听器:

  addOnBackStackChangedListener()

Performing Fragment Transactions

  使用Fragment时,可以通过用户交互来执行一些动作,比如增加、移除、替换等。

  所有这些改变构成一个集合,这个集合被叫做一个transaction。

  可以调用FragmentTransaction中的方法来处理这个transaction,并且可以将transaction存进由activity管理的back stack中,这样用户就可以进行fragment变化的回退操作。

  可以这样得到FragmentTransaction类的实例: 

FragmentManager fragmentManager = getFragmentManager();

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

每个transaction是一组同时执行的变化的集合。

  用add(), remove(), replace()方法,把所有需要的变化加进去,然后调用commit()方法,将这些变化应用。

  在commit()方法之前,你可以调用addToBackStack(),把这个transaction加入back stack中去,这个back stack是由activity管理的,当用户按返回键时,就会回到上一个fragment的状态。

  比如下面的代码就是用一个新的fragment取代之前的fragment,并且将前次的状态存储在back stack中。

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

 在这个例子中,newFragment将取代在R.id.fragment_container容器中的fragment,如果没有,将直接添加新的fragment。

  通过调用addToBackStack(),commit()的一系列转换作为一个transaction被存储在back stack中,用户按Back键可以返回上一个转换前的状态。

  当你移除一个fragment的时候,如果commit()之前没有调用addToBackStack(),那个fragment将会是destroyed;如果调用了addToBackStack(),这个fragment会是stopped,可以通过返回键来恢复。

关于commit()方法

  调用commit()方法并不能立即执行transaction中包含的改变动作,commit()方法把transaction加入activity的UI线程队列中。

  但是,如果觉得有必要的话,可以调用executePendingTransactions()方法来立即执行commit()提供的transaction。

  这样做通常是没有必要的,除非这个transaction被其他线程依赖。

  注意:你只能在activity存储它的状态(当用户要离开activity时)之前调用commit(),如果在存储状态之后调用commit(),将会抛出一个异常。

  这是因为当activity再次被恢复时commit之后的状态将丢失。如果丢失也没关系,那么使用commitAllowingStateLoss()方法。

例程序

  写了个小程序实践了一下fragment的管理,程序不是很完善,就是试试基本用法,先按第一个按钮添加一个fragment,第二个按钮将其替换,第三个按钮将第二个按钮添加的fragment删除。

  相关代码:

  第一个fragment:

ExampleFragment.java

package com.example.learningfragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ExampleFragment extends Fragment
{

//三个一般必须重载的方法
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        System.out.println("ExampleFragment--onCreate");
    }

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        System.out.println("ExampleFragment--onCreateView");
        return inflater.inflate(R.layout.example_fragment_layout, container, false);
       
    }

@Override
    public void onPause()
    {
        // TODO Auto-generated method stub
        super.onPause();
        System.out.println("ExampleFragment--onPause");
    }

@Override
    public void onResume()
    {
        // TODO Auto-generated method stub
        super.onResume();
        System.out.println("ExampleFragment--onResume");
    }

@Override
    public void onStop()
    {
        // TODO Auto-generated method stub
        super.onStop();
        System.out.println("ExampleFragment--onStop");
    }

}

它的布局:

<?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="wrap_content"
android:gravity="left"
android:textSize="20dip"
android:text="@string/fragment1"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/num1"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/num2"
/>
</LinearLayout>

第二个fragment: 

package com.example.learningfragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class NewFragment extends Fragment
{ //三个一般必须重载的方法
@Override
public void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
System.out.println("NewFragment--onCreate");
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
System.out.println("NewFragment--onCreateView");
return inflater.inflate(R.layout.new_fragment_layout, container, false); } @Override
public void onPause()
{
// TODO Auto-generated method stub
super.onPause();
System.out.println("NewFragment--onPause");
} }
<?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="wrap_content"
android:textSize="20dip"
android:gravity="left"
android:text="@string/fragment2"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/num3"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/num4"
/>
</LinearLayout>

主Activity:

LearnFragment.java

package com.example.learningfragment;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;

public class LearnFragment extends FragmentActivity
{
    Button btn1;
    Button btn2;
    Button btn3;
    ExampleFragment fragmentE;
    NewFragment fragmentN;
    FragmentManager fragmentManager;
   
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_learn_fragment);
        findViews();
        setListeners();
       
        //获得Fragment管理所需要的类的对象
        fragmentManager = getSupportFragmentManager();

}
   
    private void findViews()
    {
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
       
    }
   
    private void setListeners()
    {
        //第一个按钮,增加一个ExampleFragment
        btn1.setOnClickListener(new Button.OnClickListener()
        {

public void onClick(View v)
            {   
               
                //在程序中加入ExampleFragment               
                fragmentE = new ExampleFragment();

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.add(R.id.linear1,fragmentE);
                fragmentTransaction.addToBackStack(null);           
                fragmentTransaction.commit();               
            }
           
        }
        );
       
        //第二个按钮,用一个NewFragment替换前面增加的那个fragment
        btn2.setOnClickListener(new Button.OnClickListener()
        {

public void onClick(View v)
            {               
                fragmentN = new NewFragment();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.linear1,fragmentN);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
               
            }
           
        }
        );
       
        //第三个按钮,移除fragment
        btn3.setOnClickListener(new Button.OnClickListener()
        {

public void onClick(View v)
            {

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.remove(fragmentN);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
               
            }
           
        }
        );
    }

}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linear1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dip"
android:gravity="center_horizontal"
android:text="@string/layout1"
/>
<Button
android:id="@+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn1"
/> <fragment
android:name="com.example.learningfragment.ExampleFragment"
android:id="@+id/fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn2"
/> <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dip"
android:gravity="center_horizontal"
android:text="@string/layout2"
/>
<Button
android:id="@+id/btn3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn3"
/>
</LinearLayout> </LinearLayout>

 资源:

<resources>

    <string name="app_name">LearningFragment</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_learn_fragment">LearnFragment</string> <string name="layout1">LinearLayout1</string>
<string name="layout2">LinearLayout2</string> <string name="fragment1">FragmentType1</string>
<string name="fragment2">FragmentType2</string> <string name="num1">NO.1</string>
<string name="num2">NO.2</string>
<string name="num3">NO.3</string>
<string name="num4">NO.4</string> <string name="btn1">Add fragment</string>
<string name="btn2">Replace fragment</string>
<string name="btn3">Remove fragment</string> </resources>

程序运行截图:

参考资料:

  API Guides:Fragments

  http://developer.android.com/guide/components/fragments.html

  FragmentManager类文档:

  http://developer.android.com/reference/android/app/FragmentManager.html

  FragmentTransaction类文档

  http://developer.android.com/reference/android/app/FragmentTransaction.html

管理Fragments(转)的更多相关文章

  1. 【Android】11.5 创建和管理Fragments

    分类:C#.Android.VS2015: 创建日期:2016-02-22 一.简介 想要管理activity中的fragment,可以用FragmentManager类来实现.通过在activity ...

  2. 管理Fragments

    FragmentManager 为了管理Activity中的fragments,需要使用FragmentManager. 为了得到它,需要调用Activity中的getFragmentManager( ...

  3. 【Android 界面效果35】管理Fragments

    http://www.cnblogs.com/mengdd/archive/2013/01/09/2853254.html

  4. Android Fragment使用(二) 嵌套Fragments (Nested Fragments) 的使用及常见错误

    嵌套Fragment的使用及常见错误 嵌套Fragments (Nested Fragments), 是在Fragment内部又添加Fragment. 使用时, 主要要依靠宿主Fragment的 ge ...

  5. 1.2.1 Fragments - 碎片

    在activity中,Fragment代表了一种行为和用户界面的一部分.在一个activity里,你可以联合多个fragment来创建一个多面板的UI,你也可以在多个activity里重复使用同一个f ...

  6. 关于Fragment你所需知道的一切!

    转载自刘明渊 的博客地址:http://blog.csdn.net/vanpersie_9987 Fragment 是 Android API 中的一个类,它代表Activity中的一部分界面:您可以 ...

  7. [转]Android开发最佳实践

    ——欢迎转载,请注明出处 http://blog.csdn.net/asce1885 ,未经本人同意请勿用于商业用途,谢谢—— 原文链接:https://github.com/futurice/and ...

  8. 怎么通过activity里面的一个按钮跳转到另一个fragment(android FragmentTransaction.replace的用法介绍)

    即:android FragmentTransaction.replace的用法介绍 Fragment的生命周期和它的宿主Activity密切相关,几乎和宿主Activity的生命周期一致,他们之间最 ...

  9. Android为TV端助力 关于Fragment你所需知道的一切!

    转载自刘明渊 的博客地址:http://blog.csdn.net/vanpersie_9987 Fragment 是 Android API 中的一个类,它代表Activity中的一部分界面:您可以 ...

随机推荐

  1. java操作cookies

    建立一个无生命周期的cookie,即随着浏览器的关闭即消失的cookie,代码如下 HttpServletRequest request HttpServletResponse response Co ...

  2. iosOC可变数组选择,冒泡排序

    #pragma mark 可变数组的排序 NSMutableArray * array = [NSMutableArray arrayWithObjects: @"1",@&quo ...

  3. 常用的linux系统监控命令整理

    找到最耗CPU的java线程ps命令 命令:ps -mp pid -o THREAD,tid,time 或者 ps -Lfp pid 结果展示: 这个命令的作用,主要是可以获取到对应一个进程下的线程的 ...

  4. hdu_4734_F(x)(数位DP水)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4734 题意:给你一个F(x)的定义,然后给你a,b,问你在0到b包括b有多少个数满足F(x)<= ...

  5. reincarnation server

    - parent of all the drivers and servers - when a driver or server dies, RS collects it. - RS checks ...

  6. docker 基础命令

    检查Docker安装是否正确docker info拉取镜像docker pull (image name)启动docker run -d -d 后台运行查看日志docker logs $sample_ ...

  7. UVa11235 RMQ

    input 1<=n,q<=100000 升序序列a1 a2 a3 ... an -100000<=ai<=100000 q行i j 1<=i,j<=n 输入结束标 ...

  8. runtime基础

    前言 学习Objective-C的运行时Runtime系统是很有必要的.个人觉得,得之可得天下,失之则失天下. Objective-C提供了编译运行时,只要有可能,它都可以动态地运作.这意味着不仅需要 ...

  9. dirname(_file_) DIRECTORY_SEPARATOR

    <?php echo __FILE__ ; // 取得当前文件的绝对地址,结果:D:\www\test.php echo dirname(__FILE__); // 取得当前文件所在的绝对目录, ...

  10. 在写一个iOS应用之前必须做的7件事

    转载自:http://www.cocoachina.com/ios/20160316/15685.html 原文:https://medium.com/ios-os-x-development/7-t ...