深入分析 Android Activity (一)
接下來我們會深入分析 Activity
的一些高級特性和內部實現,包括窗口管理、生命周期管理、以及與 Fragment
的交互。
1. Activity
的窗口管理
在 Android 中,每個 Activity
都與一個 Window
相關聯。Window
是一個抽象的頂級視圖,提供了繪制和事件處理的基本框架。通常情況下,Activity
使用 PhoneWindow
類,它是 Window
的一個具體實現。
當你調用 setContentView
方法時,實際上是通過 Window
設置 Activity
的布局:
@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);
}
PhoneWindow
的實現如下:
public class PhoneWindow extends Window {@Overridepublic void setContentView(int layoutResID) {// Inflate the layout and add it to the windowmLayoutInflater.inflate(layoutResID, mContentParent);}
}
2. Activity
的生命周期管理
Activity
的生命周期由 Android 系統管理。系統在適當的時候調用 Activity
的生命周期方法,以確保 Activity
能夠正確響應用戶和系統事件。以下是一些關鍵的生命周期事件的詳細說明:
onCreate
onCreate
是 Activity
被創建時的第一個回調方法。通常在這里進行視圖的初始化和數據的加載:
@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// Initialize views and data
}
onStart
onStart
方法在 Activity
即將可見時調用。可以在這里執行一些準備操作,比如啟動動畫等:
@Override
protected void onStart() {super.onStart();// Prepare the activity to be visible
}
onResume
onResume
方法在 Activity
即將與用戶交互時調用。這里是讓 Activity
真正進入前臺,并開始交互的地方:
@Override
protected void onResume() {super.onResume();// Resume operations that were paused
}
onPause
onPause
方法在系統即將啟動另一個 Activity
時調用。在這里可以保存數據和狀態,釋放資源等:
@Override
protected void onPause() {super.onPause();// Save state and release resources
}
onStop
onStop
方法在 Activity
不再可見時調用。這里可以進一步釋放資源:
@Override
protected void onStop() {super.onStop();// Release more resources
}
onDestroy
onDestroy
方法在 Activity
被銷毀前調用。通常用于最后的清理工作:
@Override
protected void onDestroy() {super.onDestroy();// Clean up resources
}
onRestart
onRestart
方法在 Activity
從停止狀態重新啟動前調用。通常用于恢復暫停的操作:
@Override
protected void onRestart() {super.onRestart();// Restore state if needed
}
3. Activity
與 Fragment
的交互
Fragment
是 Activity
中的可復用組件,具有自己的布局和生命周期。一個 Activity
可以包含多個 Fragment
,每個 Fragment
都可以獨立處理用戶交互。
添加 Fragment
可以在 Activity
中動態添加 Fragment
:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
MyFragment fragment = new MyFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
Fragment 的生命周期
Fragment
的生命周期與 Activity
類似,但有一些額外的方法來處理與 Activity
的交互:
onAttach(Activity activity)
: 當Fragment
被附加到Activity
時調用。onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
: 創建并返回Fragment
的視圖層次結構。onActivityCreated(Bundle savedInstanceState)
: 當Activity
的onCreate
方法已經返回時調用。onDestroyView()
: 當Fragment
的視圖層次結構被移除時調用。
4. Activity
的任務和返回棧
Android 使用任務和返回棧(Back Stack)來管理 Activity
的導航。每個任務由一個棧(返回棧)來管理 Activity
。Activity
被啟動時,會被添加到任務的返回棧中。用戶按下返回按鈕時,系統會從返回棧中彈出當前的 Activity
并顯示前一個 Activity
。
可以通過 Intent
的 FLAG_ACTIVITY_*
標志來控制任務和返回棧的行為。例如:
Intent intent = new Intent(this, AnotherActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
5. 配置變化處理
當設備配置發生變化(如屏幕旋轉、語言更改等),Activity
會被銷毀并重新創建。可以通過 android:configChanges
屬性來處理某些配置變化,避免 Activity
的銷毀和重建:
<activity android:name=".MainActivity"android:configChanges="orientation|screenSize">
</activity>
在 Activity
中覆蓋 onConfigurationChanged
方法來處理配置變化:
@Override
public void onConfigurationChanged(Configuration newConfig) {super.onConfigurationChanged(newConfig);// Handle configuration changes
}
總結
Android 的 Activity
設計復雜且強大,提供了管理應用界面和用戶交互的豐富功能。通過理解其生命周期、窗口管理、與 Fragment
的交互、任務和返回棧的管理,以及配置變化處理,開發者可以創建高效、響應迅速且用戶友好的應用程序。掌握這些知識對于構建穩定和可維護的 Android 應用至關重要。