2017-04-13 86 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欢迎:) –