69

我正在尝试跨方向更改保存数据。如以下代码所示,我使用onSaveInstanceState()and onRestoreInstanceState()。我尝试获取保存的值,并检查它是否是onRestoreInstanceState(). 但是当我尝试在 中使用新值时onCreate(),我没有新值,而是旧值。

protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("TEXT", user);

    }
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    savedUser = savedInstanceState.getString("TEXT");
    Log.d("enregistred value", savedUser);

}



public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        int display_mode = getResources().getConfiguration().orientation;

        if (display_mode == 1) {

            setContentView(R.layout.main_grid);
            mGrid = (GridView) findViewById(R.id.gridview);
            mGrid.setColumnWidth(95);
            mGrid.setVisibility(0x00000000);
            // mGrid.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);

        } else {
            setContentView(R.layout.main_grid_land);
            mGrid = (GridView) findViewById(R.id.gridview);
            mGrid.setColumnWidth(95);
            Log.d("Mode", "land");
            // mGrid.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);

        }
        Log.d("savedUser", savedUser);
        if (savedUser.equals("admin")) { //value 0
            adapter.setApps(appManager.getApplications());
        } else if (savedUser.equals("prof")) { //value 1
            adapter.setApps(appManager.getTeacherApplications());
        } else {// default value
            appManager = new ApplicationManager(this, getPackageManager());
            appManager.loadApplications(true);
            bindApplications();
        }
}
4

3 回答 3

57

When your activity is recreated after it was previously destroyed, you can recover your saved state from the Bundle that the system passes your activity. Both the onCreate() and onRestoreInstanceState() callback methods receive the same Bundle that contains the instance state information.

Because the onCreate() method is called whether the system is creating a new instance of your activity or recreating a previous one, you must check whether the state Bundle is null before you attempt to read it. If it is null, then the system is creating a new instance of the activity, instead of restoring a previous one that was destroyed.

static final String STATE_USER = "user";
private String mUser;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
        // Restore value of members from saved state
        mUser = savedInstanceState.getString(STATE_USER);
    } else {
        // Probably initialize members with default values for a new instance
        mUser = "NewUser";
    }
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    savedInstanceState.putString(STATE_USER, mUser);
    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

http://developer.android.com/training/basics/activity-lifecycle/recreating.html

于 2013-05-27T09:12:31.377 回答
21
  • onSaveInstanceState()是一种用于在暂停活动之前存储数据的方法。

描述:钩子允许视图生成其内部状态的表示,以后可用于创建具有相同状态的新实例。此状态应仅包含非持久性或以后无法重建的信息。例如,您永远不会将当前位置存储在屏幕上,因为当视图的新实例放置在其视图层次结构中时,将再次计算该位置。

  • onRestoreInstanceState()是用于检索该数据的方法。

描述:当活动从先前保存的状态重新初始化时,在 onStart() 之后调用此方法,此处在 savedInstanceState 中给出。大多数实现将简单地使用 onCreate(Bundle) 来恢复它们的状态,但有时在完成所有初始化之后在这里这样做会很方便,或者允许子类决定是否使用您的默认实现。该方法的默认实现会恢复之前被 onSaveInstanceState(Bundle) 冻结的任何视图状态。

在这里考虑这个例子:
你的应用程序有 3 个编辑框,用户在其中输入一些信息,但他接到了一个电话,所以如果你没有使用上述方法,他输入的所有内容都将丢失。因此,始终将Activity 方法中
的当前数据保存为一个包,并在方法中调用该方法。onPause()onResume()onRestoreInstanceState()

请参见 :

请如何使用 onSavedInstanceState 示例

http://www.how-to-develop-android-apps.com/tag/onrestoreinstancestate/

于 2013-05-27T09:03:39.387 回答
9

发生这种情况是因为您savedValueonCreate()方法中使用了。在方法savedValue中更新onRestoreInstanceState(),但在方法onRestoreInstanceState()之后调用onCreate()。您可以:

  1. 更新savedValueinonCreate()方法,或
  2. 移动使用新savedValueinonRestoreInstanceState()方法的代码。

但我建议您使用第一种方法,使代码如下:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    int display_mode = getResources().getConfiguration().orientation;

    if (display_mode == 1) {

        setContentView(R.layout.main_grid);
        mGrid = (GridView) findViewById(R.id.gridview);
        mGrid.setColumnWidth(95);
        mGrid.setVisibility(0x00000000);
        // mGrid.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);

    } else {
        setContentView(R.layout.main_grid_land);
        mGrid = (GridView) findViewById(R.id.gridview);
        mGrid.setColumnWidth(95);
        Log.d("Mode", "land");
        // mGrid.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);

    }
    if (savedInstanceState != null) {
        savedUser = savedInstanceState.getString("TEXT");
    } else {
        savedUser = ""
    }
    Log.d("savedUser", savedUser);
    if (savedUser.equals("admin")) { //value 0
        adapter.setApps(appManager.getApplications());
    } else if (savedUser.equals("prof")) { //value 1
        adapter.setApps(appManager.getTeacherApplications());
    } else {// default value
        appManager = new ApplicationManager(this, getPackageManager());
        appManager.loadApplications(true);
        bindApplications();
    }
}
于 2015-09-05T10:29:29.497 回答