实例讲解Android应用开发中Fragment生命周期的控制

时间:2022-02-19 23:05:36

一、fragment的生命周期初探

因为fragment必须嵌入在acitivity中使用,所以fragment的生命周期和它所在的activity是密切相关的。

如果activity是暂停状态,其中所有的fragment都是暂停状态;如果activity是stopped状态,这个activity中所有的fragment都不能被启动;如果activity被销毁,那么它其中的所有fragment都会被销毁。

但是,当activity在活动状态,可以独立控制fragment的状态,比如加上或者移除fragment。

当这样进行fragment transaction(转换)的时候,可以把fragment放入activity的back stack中,这样用户就可以进行返回操作。

使用fragment时,需要继承fragment或者fragment的子类(dialogfragment, listfragment, preferencefragment, webviewfragment),所以fragment的代码看起来和activity的类似。

每当创建一个fragment时,首先添加以下三个回调方法:

oncreate():系统在创建fragment的时候调用这个方法,这里应该初始化相关的组件,一些即便是被暂停或者被停止时依然需要保留的东西。
oncreateview():当第一次绘制fragment的ui时系统调用这个方法,该方法将返回一个view,如果fragment不提供 ui也可以返回null。注意,如果继承自listfragment,oncreateview()默认的实现会返回一个listview,所以不用自己 实现。
onpause():当用户离开fragment时第一个调用这个方法,需要提交一些变化,因为用户很可能不再返回来。
将fragment加载到activity当中有两种方式:

方式一:添加fragment到activity的布局文件当中
方式二:在activity的代码中动态添加fragment(荐)
第一种方式虽然简单但灵活性不够。添加fragment到activity的布局文件当中,就等同于将fragment及其视图与activity的视图绑定在一起,且在activity的生命周期过程中,无法切换fragment视图。

第二种方式比较复杂,但也是唯一一种可以在运行时控制fragment的方式(加载、移除、替换)。

二、生命周期控制实例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.goso.testapp;
 
import android.app.activity;
import android.app.listfragment;
import android.os.bundle;
import android.util.log;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.widget.arrayadapter;
import android.widget.listview;
 
/**
 * demonstration of using listfragment to show a list of items
 * from a canned array.
 */
public class fragmentlistarray extends activity {
 
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    log.e("hjj", "activity &&&& oncreate...");
    // create the list fragment and add it as our sole content.
    if (getfragmentmanager().findfragmentbyid(android.r.id.content) == null) {
      arraylistfragment list = new arraylistfragment();
      getfragmentmanager().begintransaction().add(android.r.id.content, list).commit();
    }
  }
 
  @override
  protected void onstart() {
   // todo auto-generated method stub
   super.onstart();
   log.e("hjj", "activity &&&& onstart...");
  }
  
  @override
  protected void onresume() {
   // todo auto-generated method stub
   super.onresume();
   log.e("hjj", "activity &&&& onresume...");
  }
  
  @override
  protected void onstop() {
   // todo auto-generated method stub
   super.onstop();
   log.e("hjj", "activity &&&& onstop...");
  }
  
  @override
  protected void onpause() {
   // todo auto-generated method stub
   super.onpause();
   log.e("hjj", "activity &&&& onpause...");
  }
  
  @override
  protected void ondestroy() {
   // todo auto-generated method stub
   super.ondestroy();
   log.e("hjj", "activity &&&& ondestroy...");
  }
  
  public static class arraylistfragment extends listfragment {
 
   @override
   public void onattach(activity activity) {
   // todo auto-generated method stub
   log.e("hjj", "arraylistfragment **** onattach...");
   super.onattach(activity);
   }
   
   @override
   public void oncreate(bundle savedinstancestate) {
   // todo auto-generated method stub
   log.e("hjj", "arraylistfragment **** oncreate...");
   super.oncreate(savedinstancestate);
   }
   
   @override
   public view oncreateview(layoutinflater inflater, viewgroup container,
    bundle savedinstancestate) {
   // todo auto-generated method stub
   log.e("hjj", "arraylistfragment **** oncreateview...");
   return super.oncreateview(inflater, container, savedinstancestate);
   }
   
    @override
    public void onactivitycreated(bundle savedinstancestate) {
      super.onactivitycreated(savedinstancestate);
      log.e("hjj", "arraylistfragment **** onactivitycreated...");
      string[] array = new string[]{"c++", "java", "python"};
      setlistadapter(new arrayadapter<string>(getactivity(),
          android.r.layout.simple_list_item_1, array));
    }
 
    @override
    public void onstart() {
     // todo auto-generated method stub
     log.e("hjj", "arraylistfragment **** onstart...");
     super.onstart();
    }
    
    @override
    public void onresume() {
     log.e("hjj", "arraylistfragment **** onresume...");
     // todo auto-generated method stub
     super.onresume();
    }
    
    @override
    public void onpause() {
     log.e("hjj", "arraylistfragment **** onpause...");
     // todo auto-generated method stub
     super.onpause();
    }
    
    @override
    public void onstop() {
     log.e("hjj", "arraylistfragment **** onstop...");
     // todo auto-generated method stub
     super.onstop();
    }
    
    @override
    public void ondestroyview() {
     log.e("hjj", "arraylistfragment **** ondestroyview...");
     // todo auto-generated method stub
     super.ondestroyview();
    }
    
    @override
    public void ondestroy() {
     // todo auto-generated method stub
     log.e("hjj", "arraylistfragment **** ondestroy...");
     super.ondestroy();
    }
    
    @override
    public void ondetach() {
     log.e("hjj", "arraylistfragment **** ondetach...");
     // todo auto-generated method stub
     super.ondetach();
    }
    
    @override
    public void onlistitemclick(listview l, view v, int position, long id) {
      log.i("fragmentlist", "item clicked: " + id);
    }
  }
}

结果:
oncreate过程

?
1
2
3
4
5
01-22 15:30:28.091: e/hjj(10315): activity &&&& oncreate...
01-22 15:30:28.091: e/hjj(10315): arraylistfragment **** onattach...
01-22 15:30:28.091: e/hjj(10315): arraylistfragment **** oncreate...
01-22 15:30:28.115: e/hjj(10315): arraylistfragment **** oncreateview...
01-22 15:30:28.123: e/hjj(10315): arraylistfragment **** onactivitycreated...

onstart过程

?
1
2
01-22 15:30:28.123: e/hjj(10315): activity &&&& onstart...
01-22 15:30:28.123: e/hjj(10315): arraylistfragment **** onstart...

onresume过程

?
1
2
01-22 15:30:28.123: e/hjj(10315): activity &&&& onresume...
01-22 15:30:28.123: e/hjj(10315): arraylistfragment **** onresume...

onpause过程

?
1
2
01-22 15:31:26.748: e/hjj(10315): arraylistfragment **** onpause...
01-22 15:31:26.748: e/hjj(10315): activity &&&& onpause...

onstop过程

?
1
2
01-22 15:31:27.638: e/hjj(10315): arraylistfragment **** onstop...
01-22 15:31:27.638: e/hjj(10315): activity &&&& onstop...

onstart过程

?
1
2
01-22 15:31:57.537: e/hjj(10315): activity &&&& onstart...
01-22 15:31:57.537: e/hjj(10315): arraylistfragment **** onstart...

onresume过程

?
1
2
01-22 15:31:57.537: e/hjj(10315): activity &&&& onresume...
01-22 15:31:57.537: e/hjj(10315): arraylistfragment **** onresume...

onpause过程

?
1
2
01-22 15:32:47.412: e/hjj(10315): arraylistfragment **** onpause...
01-22 15:32:47.412: e/hjj(10315): activity &&&& onpause...

onstop过程

?
1
2
01-22 15:32:47.865: e/hjj(10315): arraylistfragment **** onstop...
01-22 15:32:47.865: e/hjj(10315): activity &&&& onstop...

ondestroy过程

?
1
2
3
4
01-22 15:32:47.865: e/hjj(10315): arraylistfragment **** ondestroyview...
01-22 15:32:47.865: e/hjj(10315): arraylistfragment **** ondestroy...
01-22 15:32:47.865: e/hjj(10315): arraylistfragment **** ondetach...
01-22 15:32:47.865: e/hjj(10315): activity &&&& ondestroy...