的onSaveInstanceState是不是救了我的价值(的onCreate输入捆绑总是空) [英] onSaveInstanceState is not saving my values ( onCreate input Bundle is always null )

查看:110
本文介绍了的onSaveInstanceState是不是救了我的价值(的onCreate输入捆绑总是空)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

保存包(活动A):

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putString("test", "value");
    super.onSaveInstanceState(outState);
}

导航至b活动;

Navigating to activity B;

startActivity(new Intent(getBaseContext(), B.class));

让我们再回到活动答:

Going back to activity A:

startActivity(new Intent(getBaseContext(), A.class));

尝试加载我写的活性的捆绑值:

Trying to load the value I wrote to bundle in activity A:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

Log.d("MY", "saved instance is null"+ Boolean.toString(savedInstanceState == null));
}

总是返回savedInstanceState = NULL。我缺少什么吗?

Returns always savedInstanceState = null. What I'm missing here?

当我回到主要活动onRestoreInstanceState永远不会触发

推荐答案

没有什么不对您的code。尝试旋转设备时的活动A.您将看到日志,这意味着你的onSaveInstanceState(以下)工作正常:

There is nothing wrong with your code. Try rotating the device when on activity A. You will see the following in the Log, which means your onSaveInstanceState() is working fine:

saved instance is nullfalse

下面是从 Android开发者网站,您可能会发现有趣的摘录:

Here are an excerpt from the Android Developer Site, which you may find interesting:

回调方法中,你可以保存你的活动的当前状态信息的onSaveInstanceState()。该系统使活动容易被破坏之前调用此方法并将其传递一个Bundle对象。捆绑在这里你可以存储有关该活动的名称 - 值对的状态信息,使用方法,如putString()。然后,如果系统杀死你的活动的进程和用户返回到你的活动,该系统通过捆绑到的onCreate(),这样你就可以恢复你的onSaveInstanceState保存过程中的活动状态()。如果没有状态信息来恢复,然后包传递到的onCreate()为null。

The callback method in which you can save information about the current state of your activity is onSaveInstanceState(). The system calls this method before making the activity vulnerable to being destroyed and passes it a Bundle object. The Bundle is where you can store state information about the activity as name-value pairs, using methods such as putString(). Then, if the system kills your activity's process and the user navigates back to your activity, the system passes the Bundle to onCreate() so you can restore the activity state you saved during onSaveInstanceState(). If there is no state information to restore, then the Bundle passed to onCreate() is null.

注:有没有保证的onSaveInstanceState()将被调用之前,你的活动被破坏,因为有些情况下,它没有必要保存状态(使用BACK键用户离开你的活动时,如的,因为用户被显式地关闭所述活动)。如果该方法被调用,它总是叫的onStop(前)和可能之前的onPause()。

Note: There's no guarantee that onSaveInstanceState() will be called before your activity is destroyed, because there are cases in which it won't be necessary to save the state (such as when the user leaves your activity using the BACK key, because the user is explicitly closing the activity). If the method is called, it is always called before onStop() and possibly before onPause().

这篇关于的onSaveInstanceState是不是救了我的价值(的onCreate输入捆绑总是空)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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