【问题标题】:Home button randomly closes activity主页按钮随机关闭活动
【发布时间】:2017-04-13 11:48:31
【问题描述】:

我应该怎么做才能防止在按下主页按钮时破坏活动。随机按 home 会破坏我的活动。

提前致谢

【问题讨论】:

  • 我们没听懂这个问题!
  • 我正在尝试纠正我的英语。但为什么要投反对票?
  • 其中一个原因是您的设备(Samsung S3)开发者设置中打开了“销毁活动”选项
  • 我没有投反对票,哈哈,

标签: java android google-maps location maps


【解决方案1】:

这是活动生命周期- 当任何应用程序的活动在按下 Home 按钮后进入后台时,它不会被任意销毁 - 当可用 RAM 不足时,操作系统会在后台销毁活动,因此当按下 Home 按钮时,活动会进入 onPause( ) -> onStop() 然后由操作系统决定。

这可能发生在任何运行 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.
        }

【讨论】:

  • 非常感谢您提供如此详细的回答。 @马坦
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多