2017-04-13 110 views
-1

我應該怎麼做以防止主頁按鈕被按下時活動被破壞。我的活動被隨機按回家銷燬。主頁按鈕隨機關閉活動

在此先感謝

+0

我們不明白的問題! –

+0

我正在嘗試糾正我的英語..但爲什麼要投票問題? –

+0

其中一個原因是您的設備(三星S3)開發人員設置 – Sergey

回答

1

這是lifecycle- 當按下Home鍵後的任何應用程序的活動轉到後臺活動,這是不能隨意破壞 - 操作系統破壞在後臺活動在運行時缺少可用的RAM,所以當Home按鈕被按下時,活動進入onPause() - > onStop(),然後取決於操作系統的仁慈。 Activity Lifecycle

這可能發生在任何運行Android操作系統的設備上,該設備在任何給定的時間都會運行在內存不足的情況下,而不僅僅是Galaxy S3。

來處理這個問題的方法是在你的活動的onSaveInstanceState使用:

@Override 
    protected void onSaveInstanceState(Bundle outState) { 
     // Put all important data you have into the outState before calling 
     // super. 
     super.onSaveInstanceState(outState); 
    } 

    @Override 
    protected void onRestoreInstanceState(Bundle state) { 
     super.onRestoreInstanceState(state); 
    // Here you will receive the bundle you put in onSaveInstanceState 
    // and you can take it from the state bundle and put it in place. 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      // Here you need to check if you have data, if 
      // onSaveInstanceState was called and you put data in that 
      // function, data should be available here, and put it back into 
      // place. 
     } 
+0

開啓「銷燬活動」選項謝謝你這麼詳細的答案。 @Matan –

+0

@BasitAli歡迎:) –