主页按钮随机关闭活动

时间:2017-04-13 11:48:32

标签: java android google-maps location maps

如何在按下主页按钮时防止活动被破坏。随便按回家,我的活动就被破坏了。

提前致谢

1 个答案:

答案 0 :(得分:1)

这是活动生命周期 - 当按下主页按钮后任何应用程序的活动进入后台时,它不会被任意破坏 - 当操作系统缺少可用内存时,操作系统会在后台销毁活动,因此当按下主页按钮时活动转到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.
        }
相关问题