Android 管理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:
1
ExampleFragment.java
2
3
package
com.xqsh.future;
4
5
import
android.os.Bundle;
6
import
android.support.v4.app.Fragment;
7
import
android.util.Log;
8
import
android.view.LayoutInflater;
9
import
android.view.View;
10
import
android.view.ViewGroup;
11
12
public
class
ExampleFragment
extends
Fragment
{
13
14 public static String TAG = "--ExampleFragment--";
15 @Override
16 public void onCreate(Bundle savedInstanceState) {
17 // TODO Auto-generated method stub
18 super.onCreate(savedInstanceState);
19 Log.v(TAG, "oncreate");
20 }
21
22 @Override
23 public View onCreateView(LayoutInflater inflater, ViewGroup container,
24 Bundle savedInstanceState) {
25 // TODO Auto-generated method stub
26 Log.v(TAG, "onCreateView");
27 return inflater.inflate(R.layout.example_fragment_layout,container,false);
28 }
29
30 @Override
31 public void onPause() {
32 // TODO Auto-generated method stub
33 super.onPause();
34 Log.v(TAG, "onPause");
35 }
36
37 @Override
38 public void onResume() {
39 // TODO Auto-generated method stub
40 super.onResume();
41 Log.v(TAG, "onResume");
42 }
43
44 @Override
45 public void onStop() {
46 // TODO Auto-generated method stub
47 super.onStop();
48 Log.v(TAG, "onStop");
49 }
50}
51
它的布局:
example_fragment_layout.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
=
"
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:
1
NewFragment.java
2
3
package
com.xqsh.future;
4
5
import
android.os.Bundle;
6
import
android.support.v4.app.Fragment;
7
import
android.util.Log;
8
import
android.view.LayoutInflater;
9
import
android.view.View;
10
import
android.view.ViewGroup;
11
12
public
class
NewFragment
extends
Fragment
{
13 public static String TAG = "--NewFragment--";
14 // 三个一般必须重载的方法
15 @Override
16 public void onCreate(Bundle savedInstanceState) {
17 // TODO Auto-generated method stub
18 super.onCreate(savedInstanceState);
19 Log.v(TAG,"NewFragment--onCreate");
20 }
21
22 @Override
23 public View onCreateView(LayoutInflater inflater, ViewGroup container,
24 Bundle savedInstanceState) {
25 Log.v(TAG,"NewFragment--onCreateView");
26 return inflater.inflate(R.layout.new_fragment_layout, container, false);
27
28 }
29
30 @Override
31 public void onPause() {
32 // TODO Auto-generated method stub
33 super.onPause();
34 Log.v(TAG,"NewFragment--onPause");
35 }
36
37}
38
它的布局:
new_fragment_layout.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
=
"
wrap_content
"
android:gravity
=
"
left
"
android:text
=
"
@string/fragment2
"
android:textSize
=
"
20dip
"
/>
<
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:
1
package
com.xqsh.future;
2
3
import
android.os.Bundle;
4
import
android.support.v4.app.FragmentActivity;
5
import
android.support.v4.app.FragmentManager;
6
import
android.support.v4.app.FragmentTransaction;
7
import
android.view.View;
8
import
android.view.View.OnClickListener;
9
import
android.widget.Button;
10
11
public
class
MainViewActivity
extends
FragmentActivity
{
12
13 private Button btn1,btn2,btn3;
14 private ExampleFragment fragmentE;
15 private NewFragment fragmentN;
16 private FragmentManager fragmentManager;
17 @Override
18 protected void onCreate(Bundle savedInstanceState) {
19 super.onCreate(savedInstanceState);
20 setContentView(R.layout.activity_main_view);
21 findViews();
22 setListeners();
23
24 //获得Fragment管理所需要的类的对象
25 fragmentManager = getSupportFragmentManager();
26 }
27
28 private void findViews(){
29 btn1 = (Button) findViewById(R.id.btn1);
30 btn2 = (Button) findViewById(R.id.btn2);
31 btn3 = (Button) findViewById(R.id.btn3);
32 }
33
34 private void setListeners(){
35 //第一个按钮,增加一个ExampleFragment
36 btn1.setOnClickListener(new Button.OnClickListener(){
37 @Override
38 public void onClick(View v) {
39 // TODO Auto-generated method stub
40 //在程序中加入ExampleFragment
41 fragmentE = new ExampleFragment();
42
43 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
44 fragmentTransaction.add(R.id.linear1,fragmentE);
45 fragmentTransaction.addToBackStack(null);
46 fragmentTransaction.commit();
47 }
48 });
49
50 btn2.setOnClickListener(new OnClickListener() {
51
52 @Override
53 public void onClick(View v) {
54 // TODO Auto-generated method stub
55 fragmentN = new NewFragment();
56 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
57 fragmentTransaction.replace(R.id.linear1, fragmentN);
58 fragmentTransaction.addToBackStack(null);
59 fragmentTransaction.commit();
60 }
61 });
62
63 btn3.setOnClickListener(new OnClickListener() {
64 @Override
65 public void onClick(View v) {
66 // TODO Auto-generated method stub
67 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
68 fragmentTransaction.remove(fragmentN);
69 fragmentTransaction.addToBackStack(null);
70 fragmentTransaction.commit();
71 }
72 });
73 }
74}
75
它的布局:
activity_main_view.xml
<
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:gravity
=
"
center_horizontal
"
android:text
=
"
@string/layout1
"
android:textSize
=
"
20dip
"
/>
<
Button
android:id
=
"
@+id/btn1
"
android:layout_width
=
"
fill_parent
"
android:layout_height
=
"
wrap_content
"
android:text
=
"
@string/btn1
"
/>
<
fragment
android:id
=
"
@+id/fragment1
"
android:name
=
"
com.xqsh.future.ExampleFragment
"
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
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:gravity
=
"
center_horizontal
"
android:text
=
"
@string/layout2
"
android:textSize
=
"
20dip
"
/>
<
Button
android:id
=
"
@+id/btn3
"
android:layout_width
=
"
fill_parent
"
android:layout_height
=
"
wrap_content
"
android:text
=
"
@string/btn3
"
/>
</
LinearLayout
>
</
LinearLayout
>
资源:
1
<?
xml version
=
"
1.0
"
encoding
=
"
utf-8
"
?>
2
<
resources
>
3
4
<
string name
=
"
app_name
"
>
LearningFragment
</
string
>
5
<
string name
=
"
hello_world
"
>
Hello world
!</
string
>
6
<
string name
=
"
menu_settings
"
>
Settings
</
string
>
7
<
string name
=
"
title_activity_learn_fragment
"
>
LearnFragment
</
string
>
8
9
<
string name
=
"
layout1
"
>
LinearLayout1
</
string
>
10
<
string name
=
"
layout2
"
>
LinearLayout2
</
string
>
11
12
<
string name
=
"
fragment1
"
>
FragmentType1
</
string
>
13
<
string name
=
"
fragment2
"
>
FragmentType2
</
string
>
14
15
<
string name
=
"
num1
"
>
NO.
1
</
string
>
16
<
string name
=
"
num2
"
>
NO.
2
</
string
>
17
<
string name
=
"
num3
"
>
NO.
3
</
string
>
18
<
string name
=
"
num4
"
>
NO.
4
</
string
>
19
20
<
string name
=
"
btn1
"
>
Add fragment
</
string
>
21
<
string name
=
"
btn2
"
>
Replace fragment
</
string
>
22
<
string name
=
"
btn3
"
>
Remove fragment
</
string
>
23
24
</
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
转载:http://www.cnblogs.com/mengdd/archive/2013/01/09/2853254.html
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
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();
2
3 package com.xqsh.future;
4
5 import android.os.Bundle;
6 import android.support.v4.app.Fragment;
7 import android.util.Log;
8 import android.view.LayoutInflater;
9 import android.view.View;
10 import android.view.ViewGroup;
11
12 public class ExampleFragment extends Fragment {
13
14 public static String TAG = "--ExampleFragment--";
15 @Override
16 public void onCreate(Bundle savedInstanceState) {
17 // TODO Auto-generated method stub
18 super.onCreate(savedInstanceState);
19 Log.v(TAG, "oncreate");
20 }
21
22 @Override
23 public View onCreateView(LayoutInflater inflater, ViewGroup container,
24 Bundle savedInstanceState) {
25 // TODO Auto-generated method stub
26 Log.v(TAG, "onCreateView");
27 return inflater.inflate(R.layout.example_fragment_layout,container,false);
28 }
29
30 @Override
31 public void onPause() {
32 // TODO Auto-generated method stub
33 super.onPause();
34 Log.v(TAG, "onPause");
35 }
36
37 @Override
38 public void onResume() {
39 // TODO Auto-generated method stub
40 super.onResume();
41 Log.v(TAG, "onResume");
42 }
43
44 @Override
45 public void onStop() {
46 // TODO Auto-generated method stub
47 super.onStop();
48 Log.v(TAG, "onStop");
49 }
50}
51
<? 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 >
2
3 package com.xqsh.future;
4
5 import android.os.Bundle;
6 import android.support.v4.app.Fragment;
7 import android.util.Log;
8 import android.view.LayoutInflater;
9 import android.view.View;
10 import android.view.ViewGroup;
11
12 public class NewFragment extends Fragment {
13 public static String TAG = "--NewFragment--";
14 // 三个一般必须重载的方法
15 @Override
16 public void onCreate(Bundle savedInstanceState) {
17 // TODO Auto-generated method stub
18 super.onCreate(savedInstanceState);
19 Log.v(TAG,"NewFragment--onCreate");
20 }
21
22 @Override
23 public View onCreateView(LayoutInflater inflater, ViewGroup container,
24 Bundle savedInstanceState) {
25 Log.v(TAG,"NewFragment--onCreateView");
26 return inflater.inflate(R.layout.new_fragment_layout, container, false);
27
28 }
29
30 @Override
31 public void onPause() {
32 // TODO Auto-generated method stub
33 super.onPause();
34 Log.v(TAG,"NewFragment--onPause");
35 }
36
37}
38
<? 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:text = " @string/fragment2 "
android:textSize = " 20dip " />
< 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 >
2
3 import android.os.Bundle;
4 import android.support.v4.app.FragmentActivity;
5 import android.support.v4.app.FragmentManager;
6 import android.support.v4.app.FragmentTransaction;
7 import android.view.View;
8 import android.view.View.OnClickListener;
9 import android.widget.Button;
10
11 public class MainViewActivity extends FragmentActivity {
12
13 private Button btn1,btn2,btn3;
14 private ExampleFragment fragmentE;
15 private NewFragment fragmentN;
16 private FragmentManager fragmentManager;
17 @Override
18 protected void onCreate(Bundle savedInstanceState) {
19 super.onCreate(savedInstanceState);
20 setContentView(R.layout.activity_main_view);
21 findViews();
22 setListeners();
23
24 //获得Fragment管理所需要的类的对象
25 fragmentManager = getSupportFragmentManager();
26 }
27
28 private void findViews(){
29 btn1 = (Button) findViewById(R.id.btn1);
30 btn2 = (Button) findViewById(R.id.btn2);
31 btn3 = (Button) findViewById(R.id.btn3);
32 }
33
34 private void setListeners(){
35 //第一个按钮,增加一个ExampleFragment
36 btn1.setOnClickListener(new Button.OnClickListener(){
37 @Override
38 public void onClick(View v) {
39 // TODO Auto-generated method stub
40 //在程序中加入ExampleFragment
41 fragmentE = new ExampleFragment();
42
43 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
44 fragmentTransaction.add(R.id.linear1,fragmentE);
45 fragmentTransaction.addToBackStack(null);
46 fragmentTransaction.commit();
47 }
48 });
49
50 btn2.setOnClickListener(new OnClickListener() {
51
52 @Override
53 public void onClick(View v) {
54 // TODO Auto-generated method stub
55 fragmentN = new NewFragment();
56 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
57 fragmentTransaction.replace(R.id.linear1, fragmentN);
58 fragmentTransaction.addToBackStack(null);
59 fragmentTransaction.commit();
60 }
61 });
62
63 btn3.setOnClickListener(new OnClickListener() {
64 @Override
65 public void onClick(View v) {
66 // TODO Auto-generated method stub
67 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
68 fragmentTransaction.remove(fragmentN);
69 fragmentTransaction.addToBackStack(null);
70 fragmentTransaction.commit();
71 }
72 });
73 }
74}
75
< 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:gravity = " center_horizontal "
android:text = " @string/layout1 "
android:textSize = " 20dip " />
< Button
android:id = " @+id/btn1 "
android:layout_width = " fill_parent "
android:layout_height = " wrap_content "
android:text = " @string/btn1 " />
< fragment
android:id = " @+id/fragment1 "
android:name = " com.xqsh.future.ExampleFragment "
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
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:gravity = " center_horizontal "
android:text = " @string/layout2 "
android:textSize = " 20dip " />
< Button
android:id = " @+id/btn3 "
android:layout_width = " fill_parent "
android:layout_height = " wrap_content "
android:text = " @string/btn3 " />
</ LinearLayout >
</ LinearLayout >
2 < resources >
3
4 < string name = " app_name " > LearningFragment </ string >
5 < string name = " hello_world " > Hello world !</ string >
6 < string name = " menu_settings " > Settings </ string >
7 < string name = " title_activity_learn_fragment " > LearnFragment </ string >
8
9 < string name = " layout1 " > LinearLayout1 </ string >
10 < string name = " layout2 " > LinearLayout2 </ string >
11
12 < string name = " fragment1 " > FragmentType1 </ string >
13 < string name = " fragment2 " > FragmentType2 </ string >
14
15 < string name = " num1 " > NO. 1 </ string >
16 < string name = " num2 " > NO. 2 </ string >
17 < string name = " num3 " > NO. 3 </ string >
18 < string name = " num4 " > NO. 4 </ string >
19
20 < string name = " btn1 " > Add fragment </ string >
21 < string name = " btn2 " > Replace fragment </ string >
22 < string name = " btn3 " > Remove fragment </ string >
23
24 </ resources >