Fragment的生命周期&同一Activity下不同Fragment之间的通信

分享到:

Android开发:碎片Fragment完全解析(2)

Fragment的生命周期

和Activity一样,Fragment也有自己的生命周期,理解Fragment的生命周期非常重要,我们通过代码的方式来瞧一瞧Fragment的生命周期是什么样的:

 1 publicclass Fragment1 extends Fragment {   
 2 publicstaticfinal String TAG = "Fragment1";   
 3 @Override 
 4 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {   
 5         Log.d(TAG, "onCreateView");   
 6 return inflater.inflate(R.layout.fragment1, container, false);   
 7     }   
 8 @Override 
 9 publicvoid onAttach(Activity activity) {   
10 super.onAttach(activity);   
11         Log.d(TAG, "onAttach");   
12     }   
13 @Override 
14 publicvoid onCreate(Bundle savedInstanceState) {   
15 super.onCreate(savedInstanceState);   
16         Log.d(TAG, "onCreate");   
17     }   
18 @Override 
19 publicvoid onActivityCreated(Bundle savedInstanceState) {   
20 super.onActivityCreated(savedInstanceState);   
21         Log.d(TAG, "onActivityCreated");   
22     }   
23 @Override 
24 publicvoid onStart() {   
25 super.onStart();   
26         Log.d(TAG, "onStart");   
27     }   
28 @Override 
29 publicvoid onResume() {   
30 super.onResume();   
31         Log.d(TAG, "onResume");   
32     }   
33 @Override 
34 publicvoid onPause() {   
35 super.onPause();   
36         Log.d(TAG, "onPause");   
37     }   
38 @Override 
39 publicvoid onStop() {   
40 super.onStop();   
41         Log.d(TAG, "onStop");   
42     }   
43 @Override 
44 publicvoid onDestroyView() {   
45 super.onDestroyView();   
46         Log.d(TAG, "onDestroyView");   
47     }   
48 @Override 
49 publicvoid onDestroy() {   
50 super.onDestroy();   
51         Log.d(TAG, "onDestroy");   
52     }   
53 @Override 
54 publicvoid onDetach() {   
55 super.onDetach();   
56         Log.d(TAG, "onDetach");   
57     }   
58 }   
昵    称:
验证码:

相关文档: