什么时候调用 onRestoreInstanceState? [英] when is onRestoreInstanceState called?

查看:42
本文介绍了什么时候调用 onRestoreInstanceState?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉我的不理解,但我是 Android 开发的新手.

Sorry for my incomprehension, but I am new in the android development.

我有一个包含活动 A 和活动 B 的应用程序,我从活动 A 转到活动 B.当我离开活动 A 时,onSaveInstanceState 方法被调用,但是当我返回时对于活动 A(来自同一应用程序中的活动 B),onCreate 方法中的包为空.

I have an application with activity A and activity B in it, and I go from activity A to activity B. When I left activity A, the onSaveInstanceState method was called, but when I went back to activity A (from activity B in the same application), the bundle in the onCreate method was null.

我该怎么做才能保存活动 A 的先前状态?我只想存储应用程序生命周期的数据.

What can I do, to save the activity A's previous state? I only want to store the data for the application lifetime.

有人可以帮我解决这个问题吗?

Can someone help me with this?

这是我的活动 A 代码:

Here is my code for Activity A:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState != null)
    {
        Log.v("Main", savedInstanceState.getString("test"));
    }
    else
    {
        Log.v("Main", "old instance");
    }
}  

@Override
public void onSaveInstanceState(Bundle savedInstanceState) 
{
    Log.v("Main", "save instance");

    savedInstanceState.putString("test", "my test string");

    super.onSaveInstanceState(savedInstanceState);
}


public void buttonClick(View view)
{
    Intent intent = new Intent(this, Activity2.class);
    startActivity(intent);
}

这是我的活动 B 代码,当我按下按钮返回活动 A 时:

Here is my code for Activity B, when I press a button to go back to activity A:

public void onBack(View view)
{
    NavUtils.navigateUpFromSameTask(this);
}

推荐答案

保存和恢复状态是为了保存当用户退出应用程序时已过时的当前临时数据.当您通过打开下一个活动来最小化或离开活动时,它可能会因资源不足而被系统杀死,并在您返回时使用 savedInstanceState 重新启动.所以 onSaveInstanceState() 仅用于保存最小化恢复会话数据.

Saving and restoring state is meant to save the current temporary data that is obsolete when user exits the application. When you minimize or leave the Activity by opening next one it might be killed by the system due to lack of resources and restarted with savedInstanceState when you get back to it. So use onSaveInstanceState() only for saving minimize-restore session data.

因此,如果您在前面启动一个新的 Activity 并返回到前一个(您正在尝试执行的操作),则 Activity A 可能不会被杀死(只是停止)并重新启动而不会被销毁.您可以通过在开发者选项菜单中选中 Don't keep activity 来强制杀死它并恢复.

So if you start a new Activity in front and get back to the previous one (what you are trying to do), the Activity A might not be killed (just stopped) and restarted without going being destroyed. You can force killing it and restoring by checking Don't keep activities in developer options menu.

如果您调用 finish() 或从最近的任务列表中删除 ActivitysavedInstanceState 将不会传递给 onCreate() 因为任务被清除了.

If you call finish() or remove the Activity from recent task list the savedInstanceState will not be passed to onCreate() since the task was cleared.

如果值必须是持久的,请考虑使用 SharedPreferences.

If the value must be persistent consider using SharedPreferences.

这篇关于什么时候调用 onRestoreInstanceState?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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