[英]Is there a way to check if Activity is onStop status or onPause status ?
我需要在onNewIntent函數中處理某些事情,當當前Activity為onStop或onPause狀態時,我想做一些不同的事情。
onStop狀態我的意思是onStop剛剛被調用,活動保持在此狀態。
onPause狀態類似。
有沒有辦法檢查它是處於onStop狀態還是onPause狀態?
似乎isDestroyed()可以檢查onDestory是否被調用,onPause和onStop是否具有類似的功能?
謝謝〜
您可以在onStop或onPause事件中設置共享狀態變量。
然后在需要時檢索它。
// In your declarations:
protected static int status = 0;
// ...
@Override
protected void onPause()
{
// TODO Auto-generated method stub
super.onPause();
staus = 1;
}
@Override
protected void onStop()
{
// TODO Auto-generated method stub
super.onStop();
staus = 2;
}
// Then, wherever in your code (even in other activities) you can check the
// value of status. And even reset it to its initial value, 0
沒有內置函數來確定活動處於哪種狀態。 如果您確實需要,可以使用標志來指示“活動”處於哪個狀態,或者在此處查找更好的解決方案。
在您的活動課程中使用此功能將對您有所幫助
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Toast.makeText(getApplicationContext(), "On Pause", Toast.LENGTH_SHORT).show();
// write code whatever you want to write here
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Toast.makeText(getApplicationContext(), "On Stop", Toast.LENGTH_SHORT).show();
// write code whatever you want to write here
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.