如何区分的onDestroy()是否被称为的配置更改序列的一部分? [英] How to distinguish whether onDestroy() is called as part of configuration change sequence?

查看:177
本文介绍了如何区分的onDestroy()是否被称为的配置更改序列的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的活动一些外在的东西(服务)需要的onDestroy被销毁()。但我不希望这个配置时发生变更(如键盘翻转出来),因为它会立即恢复。

所以,问题是:如何区分的onDestroy()是否由说返回键preSS或配置变化过程的一部分

在@ CommonsWare的回答将是pretty的简单)是这样的:

  @覆盖
onDestry(){
  如果(mIsChangeConfig ==真){
    mIsChangeConfig = FALSE:
  } 其他 {
    stopService();
  }
}

@覆盖
onRetainNonConfigurationInstance(){
  mIsChangeConfig =真;
}
 

解决方案

在安卓3.X(API等级11),你可以调用 isChangingConfigurations()如果看到该项活动由于配置改变破坏。

在此之前,覆盖 onRetainNonConfigurationInstance(),并设置一个布尔值数据成员(例如, isChangingConfigurations )以,并检查的onDestroy的数据成员()

In my Activity some external thing (service) need to be destroyed in onDestroy(). But I do not want this when configuration change happens (e.g. keyboard flips out) because it will be restored right away.

So the question is: how to distinguish whether onDestroy() is caused by say Back-key press or part of config change process?

after @CommonsWare's answer it would be pretty simple) something like:

@override 
onDestry() { 
  if (mIsChangeConfig == true) { 
    mIsChangeConfig = false: 
  } else { 
    stopService(); 
  } 
} 

@override 
onRetainNonConfigurationInstance() { 
  mIsChangeConfig = true; 
}

解决方案

In Android 3.x (API Level 11), you can call isChangingConfigurations() to see if the activity is being destroyed due to a configuration change.

Prior to that, override onRetainNonConfigurationInstance() and set a boolean data member (e.g., isChangingConfigurations) to true, and check that data member in onDestroy().

这篇关于如何区分的onDestroy()是否被称为的配置更改序列的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆