2014-05-08 58 views
-1

我需要處理onNewIntent函數中的某些內容,我想在當前Activity處於onStop或onPause狀態時做一些不同的事情。有沒有辦法檢查活動是onStop狀態還是onPause狀態?

onStop狀態我的意思是onStop只是叫,活動停留在這種狀態。

onPause狀態類似。

有沒有辦法檢查它是onStop狀態還是onPause狀態?

似乎isDestroyed()可以檢查是否onDestory被調用,是否有onPause和onStop類似的函數?

謝謝〜

回答

0

您可以在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 
+0

看來這是一種解決方法,它不能是靜態變量,因爲多個實例將共享。 還認爲這不是一個好的解決方案,因爲保持這個變量值是不容易的(也需要在onResume,onStart,onRestart,onDestory和其他地方設置這個值)。 – zhibin

0

沒有構建函數來確定活動在哪個狀態。如果您真的需要這樣做,您可以使用標誌來指示活動是在哪個狀態下進行的,或者看看here以獲得更好的解決方案。

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 
     } 
+0

我不想在onPause和onStop回調中做這些事情,我只想檢查當前活動是否處於這種狀態。 – zhibin

+0

你想知道你的活動是暫停還是其他? –

相關問題