Android的SaveInstanceState - 了解 [英] Android SaveInstanceState - Understanding

查看:164
本文介绍了Android的SaveInstanceState - 了解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在该页面中的<一个href="http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState%28android.os.Bundle%29"相对=nofollow> Android SDK中

默认实现处理大多数的UI每个实例的状态,你通过在有一个ID,该层次中的每个视图调用的onSaveInstanceState(),并通过保存当前集中视图的ID(所有这些是通过onRestoreInstanceState(捆绑))的默认实现恢复。

The default implementation takes care of most of the UI per-instance state for you by calling onSaveInstanceState() on each view in the hierarchy that has an id, and by saving the id of the currently focused view (all of which is restored by the default implementation of onRestoreInstanceState(Bundle)).

那么,有没有自动保存的活动状态,而不从活动的每一个元素节约价值的机制?我感到困惑上述表示的。

So is there a mechanism that automatically saves the Activity state without saving value from each element in the activity? I am confused about the above statement.

有关的例子,活动一个调用活动B.在活动B,我有checboxes,单选按钮等。用户选择自己的选择,然后单击后退按钮。我展示活动在这一点上,我要保存的用户选择。当用户再次回来,从活动A到B,通过点击一个按钮,我希望看到所有选择坚持。一种方法我能想到的是,设置意图旗带来的活动,以脱颖而出。但不是推荐的方法,我想。

For an example, Activity A invoked Activity B. In Activity B, I have checboxes, Radio Buttons, etc. User select their choices and click Back button. I am showing the Activity At this point, I want to save the user selection. When user again comes back from Activity A to B, by clicking a button, I would like to see all selections persisted. One way I can think of is, setting the Intent Flag to bring the Activity to fore. But not a recommended method, I think.

那么,有没有一个默认的实现来保存状态,每次从SDK上面的文字?或者可能是我除preting错了?

So is there a default implementation to save the state, per the above text from SDK? Or may be I am interpreting it wrong?

推荐答案

的onSaveInstanceState() onRestoreInstanceState()仅通过Android的显式调用时需要活动来重新创建,一般配置更改后(例如改变方向)。这并不能掩盖的情况下,当你调用活动的一个新的实例。当你preSS后退按钮,活动B被破坏,要创建它的一个新实例的下一次启动活动

onSaveInstanceState() and onRestoreInstanceState() are only explicitly called by Android when the Activity needs to be recreated, generally after a configuration change (ex. changing orientation). This doesn't cover the case when you have invoked a new instance of the Activity. When you press the back button, Activity B is destroyed, and you are creating a new instance of it the next time you start that Activity.

如果您要手动保存活动的实例,通过 startActivityForResult调用活动B()。然后,在活动B,覆盖的onDestroy()的方法,并调用code这几行:

If you want to manually save the instance of an Activity, invoke Activity B via startActivityForResult(). Then, in Activity B, override the onDestroy() method, and call these lines of code:

@Override
protected void onDestroy() {

    Bundle savedState = new Bundle();
    onSaveInstanceState(savedState);
    Intent data = new Intent();
    data.putExtra("savedState", savedState);
    setResult(RESULT_OK, data);

    super.onDestroy();
}

在活动A,覆盖的 onActivityResult 并保存数据:

In Activity A, override on onActivityResult and save the data:

Bundle activityBData;

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK) {
        activityBData = data.getBundleExtra("saved_state");
    }
}

然后,重新启动b活动时,调用它像这样:

Then, when starting Activity B again, call it like so:

Intent intent = new Intent(this, ActivityB.class);
if (activityBData != null) {
    intent.putExtra("saved_state", activityBData);
}
startActivityForResult(intent, 0);

最后,在活动B的的onCreate 方法,恢复状态:

if (savedInstanceState == null) {
    Intent intent = getIntent();
    Bundle savedState = intent.getBundleExtra("saved_state");
    onRestoreInstanceState(savedState);
}

这篇关于Android的SaveInstanceState - 了解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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