Android入门(五) | Activity 的生命周期
文章目錄
- Activity 的狀態(tài)及生命周期
- 實(shí)現(xiàn)管理生命周期
- FirstActivity
- SecondActivity
- DialogActivity
- 運(yùn)行結(jié)果
- 舊活動(dòng)被回收了還能返回嗎?
Activity 的狀態(tài)及生命周期
Android 的應(yīng)用程序運(yùn)用 棧(Back Stack) 的思想來(lái)管理 Activity:
- 每創(chuàng)建一個(gè)新活動(dòng),就會(huì)覆蓋在舊活動(dòng)之上,相當(dāng)于壓入棧。
- 每當(dāng)按下 返回鍵(Back) 或者調(diào)用 finish() ,就會(huì)銷毀棧頂?shù)?Activity,相當(dāng)于彈出棧。
Activity 有四種狀態(tài):
- 運(yùn)行:在棧頂時(shí)運(yùn)行。
- 暫停:不再處于棧頂、但屏幕上可見(jiàn)時(shí)暫停。
- 停止:不再處于棧頂且屏幕上不可見(jiàn)時(shí)停止。
- 銷毀:彈棧后銷毀。
Activity 類定義了七個(gè)回調(diào)方法,覆蓋了整個(gè) Activity 生命周期:
- onCreate() :活動(dòng)首次創(chuàng)建時(shí);
- onStart() :活動(dòng)由不可見(jiàn)變?yōu)榭梢?jiàn);
- onResume() :活動(dòng)位于棧頂且準(zhǔn)備好與用戶交互;
- onPause() :在系統(tǒng)準(zhǔn)備去啟動(dòng)或者恢復(fù)另一個(gè)活動(dòng)時(shí)調(diào)用,通常會(huì)釋放一些占用 CPU 的資源,保存一些關(guān)鍵數(shù)據(jù);
- onStop() :活動(dòng)完全不可見(jiàn)時(shí)調(diào)用,如果新活動(dòng)不是對(duì)話框式則調(diào)用 onStop,否則調(diào)用 onPause;
- onDestroy() :將活動(dòng)變?yōu)殇N毀狀態(tài);
- onRestart() :將活動(dòng)由停止變?yōu)檫\(yùn)行。
實(shí)現(xiàn)管理生命周期
創(chuàng)建三個(gè) Activity 文件以實(shí)現(xiàn)研究生命周期。
FirstActivity
FirstActivity 中添加兩個(gè)按鈕分別實(shí)現(xiàn)啟動(dòng) 普通活動(dòng) 和 對(duì)話框式活動(dòng),并添加七個(gè)生命周期方法:
public class FirstActivity extends AppCompatActivity {private static final String TAG = "FirstActivity";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.first_layout);Button button_normal = (Button)findViewById(R.id.button_normal);button_normal.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(FirstActivity.this,SecondActivity.class);startActivity(intent);}});Button button_dialog = (Button)findViewById(R.id.button_dialog);button_dialog.setOnClickListener((View view)->{Intent intent = new Intent(this, DialogActivity.class);startActivity(intent);});}@Overrideprotected void onStart() {super.onStart();Log.d(TAG, "onStart: ");}@Overrideprotected void onResume() {super.onResume();Log.d(TAG, "onResume: ");}@Overrideprotected void onPause() {super.onPause();Log.d(TAG, "onPause: ");}@Overrideprotected void onStop() {super.onStop();Log.d(TAG, "onStop: ");}@Overrideprotected void onDestroy() {super.onDestroy();Log.d(TAG, "onDestroy: ");}@Overrideprotected void onRestart() {super.onRestart();Log.d(TAG, "onRestart: ");} }first_layout.xml 實(shí)現(xiàn) FirstActivity 的具體布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:id="@+id/button_normal"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="normal"></Button><Buttonandroid:id="@+id/button_dialog"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="dialog"></Button></LinearLayout>SecondActivity
SecondActivity 作為一個(gè)普通活動(dòng),供 FirstActivity 的 button_normal 跳轉(zhuǎn)調(diào)用,其 button2 提供一個(gè)能撥號(hào) 10086 的 intent:
public class SecondActivity extends AppCompatActivity {private static final String TAG = "SecondActivity";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.second_layout);Button button2 = findViewById(R.id.button_2);button2.setOnClickListener((View view)->{Intent intent = new Intent(Intent.ACTION_DIAL);intent.setData(Uri.parse("tel:10086"));startActivity(intent);});} }second_layout.xml :
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:orientation="vertical"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".SecondActivity"><Buttonandroid:id="@+id/button_2"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="button_2"/></LinearLayout>DialogActivity
創(chuàng)建 DialogActivity 文件,旨在實(shí)現(xiàn)一個(gè)對(duì)話框式的活動(dòng):
public class DialogActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.dialog_layout);} }想要讓其實(shí)現(xiàn)對(duì)話框式,需要對(duì)其 activity 標(biāo)簽做出如下修改:
通過(guò) android:theme 指定 DialogActivity 的主題。
PS: 書(shū)上是這么寫(xiě)的 android:theme="@android:style/Theme.Dialog" ,這是 Activity 的 theme,但我們默認(rèn)生成的 DialogActivity 繼承的是 AppCompatActivity,所以就要使用與其配合的 AppCompat 的 theme 才行。
dialog_layout.xml 文件設(shè)計(jì)布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:text="Dialog Activity"/></LinearLayout>運(yùn)行結(jié)果
點(diǎn)擊運(yùn)行,首先顯示的是 FirstActivity 的界面:
此時(shí)查看 logcat,生命周期執(zhí)行到 onResume 階段:
點(diǎn)擊 NORMAL 按鈕,跳轉(zhuǎn)到 SecondActivity 界面:
因此 onPause() —— FirstActivity 啟動(dòng)另外一個(gè)活動(dòng) SecondActivity 和 onStop() —— SecondActivity 需要占滿整個(gè)屏幕,因此 FirstActivity 停止,都會(huì)被執(zhí)行:
按下 Back 鍵返回 FirstActivity 界面,觀察 logcat:
由于 FirstActivity 已經(jīng)進(jìn)入停止?fàn)顟B(tài),因此執(zhí)行 onRestart():由停止變?yōu)檫\(yùn)行,之后重新執(zhí)行 onStart() :由不可見(jiàn)變?yōu)榭梢?jiàn) 和 onResume :準(zhǔn)備好與用戶交互。之所以不執(zhí)行 onCreate() ,是因?yàn)?FirstActivity 沒(méi)有重新創(chuàng)建。
點(diǎn)擊 DIALOG 按鈕,跳轉(zhuǎn)到 DialogActivity 界面:
此時(shí) logcat 只有一條日志信息:
沒(méi)有調(diào)用 onPause() 是因?yàn)?DialogActivity 并沒(méi)有占滿整個(gè)屏幕,FirstActivity 只是進(jìn)入了暫停狀態(tài),沒(méi)有進(jìn)入停止?fàn)顟B(tài)。
同理按下 Back 鍵也只會(huì)執(zhí)行 onResume() 方法,因?yàn)椴挥脧耐V範(fàn)顟B(tài)重新變?yōu)檫\(yùn)行。
最后在 FirstActivity 界面點(diǎn)擊 Back 鍵退出程序,會(huì)依次執(zhí)行:
之后便銷毀了 FirstActivity。
舊活動(dòng)被回收了還能返回嗎?
A 活動(dòng)啟動(dòng) B 活動(dòng),假如此時(shí)內(nèi)存空間不足,系統(tǒng)將 A 活動(dòng)進(jìn)行回收了,這時(shí)點(diǎn)擊 Back 還能返回 A 嗎?
其實(shí) A 還是會(huì)正常顯示的,只不過(guò)這時(shí)并不會(huì)執(zhí)行 onRestart() 方法,而是執(zhí)行 oncreate() 方法,A 會(huì)被重新創(chuàng)建一次。唯一的問(wèn)題是 A 中暫存的臨時(shí)數(shù)據(jù)都丟失了。
Activity 中提供了一個(gè) onSaveInstanceState 的回調(diào)方法,活動(dòng)在被回收之前一定會(huì)調(diào)用該方法,從而解決活動(dòng)被回收時(shí)臨時(shí)數(shù)據(jù)得不到保存的問(wèn)題。
@Override protected void onSaveInstanceState(@NonNull Bundle outState) {super.onSaveInstanceState(outState); }將一些重要的臨時(shí)數(shù)據(jù)保存在 Bundle 中,然后在 onCreate 的 Bundle 變量中提取數(shù)據(jù)。
onCreate 的 Bundle 一般都為 null ,但是如果在活動(dòng)被系統(tǒng)回收之前有通過(guò) onSaveInstanceState() 方法來(lái)保存數(shù)據(jù)的話,這個(gè)參數(shù)就會(huì)帶有之前所保存的全部數(shù)據(jù),我們只需要再通過(guò)相應(yīng)的取值方法將數(shù)據(jù)取出即可。
總結(jié)
以上是生活随笔為你收集整理的Android入门(五) | Activity 的生命周期的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 智能指针
- 下一篇: android sina oauth2.