android 弹窗 onpause,Android 下拉通知栏时Activity的生命周期——重新理解onPause()
下拉通知欄時(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)沒有日志打印,說明下拉通知欄對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ì)對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類型的對話框,然后按返回鍵。
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的android 弹窗 onpause,Android 下拉通知栏时Activity的生命周期——重新理解onPause()的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: FMDB简介
- 下一篇: seekbar垂直放置_将刻度线对准An