編輯:關於Android編程
一、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的方式(加載、移除、替換)。
二、生命周期控制實例
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過程
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過程
01-22 15:30:28.123: E/HJJ(10315): Activity &&&& onStart... 01-22 15:30:28.123: E/HJJ(10315): ArrayListFragment **** onStart...
onResume過程
01-22 15:30:28.123: E/HJJ(10315): Activity &&&& onResume... 01-22 15:30:28.123: E/HJJ(10315): ArrayListFragment **** onResume...
onPause過程
01-22 15:31:26.748: E/HJJ(10315): ArrayListFragment **** onPause... 01-22 15:31:26.748: E/HJJ(10315): Activity &&&& onPause...
onStop過程
01-22 15:31:27.638: E/HJJ(10315): ArrayListFragment **** onStop... 01-22 15:31:27.638: E/HJJ(10315): Activity &&&& onStop...
onStart過程
01-22 15:31:57.537: E/HJJ(10315): Activity &&&& onStart... 01-22 15:31:57.537: E/HJJ(10315): ArrayListFragment **** onStart...
onResume過程
01-22 15:31:57.537: E/HJJ(10315): Activity &&&& onResume... 01-22 15:31:57.537: E/HJJ(10315): ArrayListFragment **** onResume...
onPause過程
01-22 15:32:47.412: E/HJJ(10315): ArrayListFragment **** onPause... 01-22 15:32:47.412: E/HJJ(10315): Activity &&&& onPause...
onStop過程
01-22 15:32:47.865: E/HJJ(10315): ArrayListFragment **** onStop... 01-22 15:32:47.865: E/HJJ(10315): Activity &&&& onStop...
onDestroy過程
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...
There are many great advantages to building your own UI components, such as the abili
廢話不多說了,直接給大家貼代碼了。package com.example.testactivityresquest; import android.app.Activit
最近我嘗試使用ViewPager+GridView實現的,看起來一切正常,廢話不多說,具體代碼如下:如圖是效果圖 首先分析下思路1.首先是怎麼布局:整體是一個V
Android4.3按鍵消息處理與之前的版本有稍微的區別,基本原理還是一樣的,這裡主要從兩個階段來分析: 1.前期的准備工作,即開機時啟動相應的的線程,靜候按鍵事件的來臨