下拉通知欄時(shí)發(fā)生了什么
在某個(gè)APP中佃乘,發(fā)現(xiàn)下拉通知欄的時(shí)候,正在播放的視頻會(huì)暫停,于是有點(diǎn)好奇這段操作是不是在生命周期中實(shí)現(xiàn)的骂远。在網(wǎng)上眾多關(guān)于Activity生命周期的討論中,很多人認(rèn)為onPause()
和onStop()
的區(qū)別就是“部分遮擋”和“全部遮擋”腰根,那按照這個(gè)猜測來分析一下這個(gè)過程:
首先激才,通知欄下拉一點(diǎn)點(diǎn),符合一般描述中“Activity被部分遮擋”——onPause()
然后额嘿,通知欄完全落下之后瘸恼,“Activity被全部遮擋”——onStop()
于是自己寫了一個(gè)實(shí)例來驗(yàn)證:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "onCreate");
}
@Override
protected void onStart() {
super.onStart();
Log.i(TAG, "onStart");
}
@Override
protected void onResume() {
super.onResume();
Log.i(TAG, "onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.i(TAG, "onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.i(TAG, "onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy");
}
}
啟動(dòng)APP時(shí),毫無疑問册养,調(diào)用了onCreate()
→onStart()
→onResume()
东帅;
完全下拉通知欄,然后上拉通知欄球拦,發(fā)現(xiàn)沒有日志打印靠闭,說明下拉通知欄對(duì)Activity的生命周期沒有影響帐我。
其他的“部分遮擋”——AlertDialog、Toast
經(jīng)過測試不難發(fā)現(xiàn)愧膀,在Activity中彈出AlertDialog拦键、Toast時(shí),Activity的onPause()
并沒有調(diào)用檩淋;筆者還嘗試在MIUI系統(tǒng)中喚醒小愛同學(xué)矿咕,發(fā)現(xiàn)onPause()
仍然沒有被調(diào)用。
但是在以下特殊的情況下狼钮,onPause()
會(huì)被調(diào)用:
- 自定義dialog繼承自Activity
- 新啟動(dòng)的Activity主題設(shè)置為
android:theme=@android:style/Theme.Dialog
重新理解onPause()
跑去看文檔發(fā)現(xiàn)了如下信息:
Method | Description |
---|---|
onPause() | Called when the system is about to start resuming a previous activity. This is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc. Implementations of this method must be very quick because the next activity will not be resumed until this method returns. Followed by either onResume() if the activity returns back to the front, or onStop() if it becomes invisible to the user. |
發(fā)現(xiàn)了onPause()
和Activity的奇妙聯(lián)系碳柱,就不難理解之前為什么沒有被調(diào)用的問題了。
查看AlertDialog和Toast的源碼熬芜,可以發(fā)現(xiàn)它們顯示的原理莲镣,都是通過WindowManager.addView()
來顯示的。也就是說涎拉,AlertDialog和Toast可以看做是當(dāng)前Activity的一部分View瑞侮,當(dāng)然也不會(huì)對(duì)Activity的生命周期構(gòu)成影響。
因此鼓拧,onPause()
是否調(diào)用的關(guān)鍵就是半火,是否有另一個(gè)Activity參與進(jìn)來了。
而網(wǎng)上流傳甚廣的onPause()
和onStop()
調(diào)用中提到的“遮擋”季俩,應(yīng)該修正為“被Activity遮擋”
至于官方文檔中提到的钮糖,onPause()
之后會(huì)調(diào)用onStop()
或者onResume()
,前者很好理解酌住,一般的退出店归、新啟動(dòng)一個(gè)全屏Activity、鎖屏酪我、返回HOME等操作都是這種情況消痛;至于后者,筆者能想到的情況就是都哭,彈出部分遮擋的Activity類型的對(duì)話框秩伞,然后按返回鍵。