如何保存/恢复(更新)裁判对话屏幕旋转过程中?(我需要在活动的onCreate方法参考) [英] How to save/restore(update) ref to the dialog during screen rotation?(I need ref in onCreate method of activity.)

查看:151
本文介绍了如何保存/恢复(更新)裁判对话屏幕旋转过程中?(我需要在活动的onCreate方法参考)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected Dialog onCreateDialog(int id) {
...
AlertDialog.Builder adb = new AlertDialog.Builder(this);
...
mydialog = adb.create();
...
}

但onCreateDialog的onCreate后运行。

But onCreateDialog runs after onCreate.

推荐答案

如果你想成为向下兼容的,你这样做如下:

If you want to be backwards compatible, you do it as follows.

class MyActivity extends Activity {
  protected static final class MyNonConfig {
    // fill with public variables keeping references and other state info, set to null
  }
  private boolean isConfigChange;
  private MyNonConfig nonConf;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_layout);
    nonConf = (MyNonConfig)getLastNonConfigurationInstance();
    if (nonConf == null) { nonConf = new NonConfig(); }
    // handle the information of the nonConf objects/variables
    ...
  }
  @Override
  protected void onStop() {
    super.onStop();
    isConfigChange = false;
  }
  @Override
  public Object onRetainNonConfigurationInstance() {
    isConfigChange = true;
    return nonConf;
  }
  @Override
  protected void onDestroy() {
    super.onDestroy();
    // handle objects in nonConf, potentially based on isConfigChange flag
    ...
  }

该nonConf对象将生存的所有配置更改(但不是你的应用程序的真正站)。此外,isConfigChange标志告诉你的可靠的,如果你的行为将被立即或不重新创建。因此,你可以取消/分离任务或基于此信息充分地处理其他资源。

The nonConf objects will survive all configuration changes (but not real stops of your app). Also, the isConfigChange flag tells you reliably if your activity is going to be re-created immediately or not. Thus, you can cancel/detach tasks or handle other resources adequately based on this information.

编辑:请注意的如果的onDestroy()被称为然后的你可以依靠 isConfigChange 标记。此外,的如果的Andr​​oid正在处理的配置变化的然后的onDestroy()将会的被称为。然而,的如果的Andr​​oid是即将结束你的活动,然后调用的onDestroy()方法的可选的,因为Android的考虑您的活动是killable正确调用的onPause()(pre-蜂窝)或的onStop()(蜂窝及以后)。这不是一个问题,但,因为如果你的活动会被杀害,你的众多对象的状态是不感兴趣的任何人了。但是,如果你想成为友好的,这是一个更多的方面来考虑有关如何付诸的onPause()的onStop()的决定

Please note that if onDestroy() is called then you can rely on the isConfigChange flag. Also, if Android is processing a configuration change then onDestroy() will be called. However, if Android is about to end your activity then the call to onDestroy() is optional because Android considers your activity to be killable right after it calls onPause() (pre-Honeycomb) or onStop() (Honeycomb and beyond). That's not a problem though, because if your activity is going to be killed, the state of your numerous objects isn't interesting to anyone anymore. However, if you want to be friendly, this is one more aspect to consider regarding the decision what to put into onPause() and onStop().

希望这有助于。

这篇关于如何保存/恢复(更新)裁判对话屏幕旋转过程中?(我需要在活动的onCreate方法参考)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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