当onRestoreInstanceState叫什么名字?叫什么名字、onRestoreInstanceState

2023-09-04 08:38:33 作者:疯就要疯的够独特

对不起,我的科钦prehension,但我相信新的Andr​​oid开发。

我有活性的和活动中的B是一个应用程序,和我一起从活动A到活动B.当我离开活性的,在的onSaveInstanceState 方法被调用,但是当我回到活性的(从同一个应用程序活动B),包在的onCreate 方法为null。

我能做些什么,以节省的活动时的previous状态?我只想存储数据的应用程序生命周期。

有人可以帮助我?

下面是我的$ C $下活动答:

  @覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);

    如果(savedInstanceState!= NULL)
    {
        Log.v(主,savedInstanceState.getString(测试));
    }
    其他
    {
        Log.v(主,旧实例);
    }
}

@覆盖
公共无效的onSaveInstanceState(包savedInstanceState)
{
    Log.v(主,拯救实例);

    savedInstanceState.putString(测试,我的测试字符串);

    super.onSaveInstanceState(savedInstanceState);
}


公共无效buttonClick(查看视图)
{
    意向意图=新的意图(这一点,Activity2.class);
    startActivity(意向);
}
 

下面是我的$ C $下的活动B,当我preSS一个按钮返回到活动答:

 公共无效onBack(查看视图)
{
    NavUtils.navigateUpFromSameTask(本);
}
 
这些表情包叫什么名字

解决方案

保存和恢复状态,是为了保存当前的临时数据已经过时,当用户退出应用程序。 当最小化或打开下一个可能被系统杀死由于缺乏资源,并重新启动与 savedInstanceState 当你回到它退出活动。所以,使用的onSaveInstanceState()只保存最小化,还原会话数据。

如果你开始在前面一个新的活动,并返回到previous一(你正在尝试做的),那么,活动A可能不会被杀死(只是停止)并重新启动,而无需被破坏。您可以强制杀死它,并通过检查不要让在开发人员选项菜单中的活动恢复。

如果你调用完成()或删除活动从近期的任务列表中的 savedInstanceState 将不会被传递到的onCreate(),因为任务已被清除。

如果该值必须是持续性考虑使用共享preferences。

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

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.

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?

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);
}

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);
}

解决方案

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.

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.

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.

If the value must be persistent consider using SharedPreferences.