2017-04-13 4 views
-1

ホームボタンを押したときにアクティビティが破壊されないようにするにはどうすればよいですか。私の活動は家をランダムに押すことによって破壊されます。任意のアプリケーションの活動は、ホームボタンを押した後、それを任意に破壊されていないバックグラウンドに移行すると、事前ホームボタンがランダムにアクティビティを終了します

+0

私たちは質問を理解していませんでした! –

+0

私は英語を訂正しようとしています。 –

+0

あなたのデバイス(Samsung S3)のデベロッパー設定で "アクティビティを破棄する"オプションが有効になっています – Sergey

答えて

1

おかげでこれは lifecycle-活動である - それは実行時にOSは、バックグラウンドでの活動を破壊します使用可能なRAMが不足しているため、ホームボタンを押すと、アクティビティはonPause() - > onStop()に移動し、OSの慈悲に従います。 Activity Lifecycle

これは、Galaxy S3だけでなく、いつでも低メモリシナリオで実行されるAndroid OSを実行しているすべてのデバイスで発生します。

これを処理する方法は、あなたの活動に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あなたは歓迎です:) –

関連する問題