如何使用的onSaveInstanceState

如何使用的onSaveInstanceState

本文介绍了如何使用的onSaveInstanceState()和onRestoreInstanceState()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图拯救整个方向改变的数据。这表现在以下code,我用的onSaveInstanceState() onRestoreInstanceState()。我试图让保存的价值,我检查它是否在 onRestoreInstanceState正确的值()。但是,当我尝试使用新的值的onCreate(),我没有新的价值,但旧的。

 保护无效的onSaveInstanceState(包outState){
        super.onSaveInstanceState(outState);
        outState.putString(TEXT,用户);

    }
保护无效onRestoreInstanceState(包savedInstanceState){
    super.onRestoreInstanceState(savedInstanceState);
    savedUser = savedInstanceState.getString(TEXT);
    Log.d(enregistred价值,savedUser);

}



公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        INT display_mode = getResources()getConfiguration()方向。;

        如果(display_mode == 1){

            的setContentView(R.layout.main_grid);
            MGRID =(GridView控件)findViewById(R.id.gridview);
            mGrid.setColumnWidth(95);
            mGrid.setVisibility(00000000);
            // mGrid.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);

        } 其他 {
            的setContentView(R.layout.main_grid_land);
            MGRID =(GridView控件)findViewById(R.id.gridview);
            mGrid.setColumnWidth(95);
            Log.d(模式,土地);
            // mGrid.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);

        }
        Log.d(savedUser,savedUser);
        如果(savedUser.equals(管理员)){//值0
            adapter.setApps(appManager.getApplications());
        }否则,如果(savedUser.equals(教授)){//值1
            adapter.setApps(appManager.getTeacherApplications());
        }其他{//默认值
            AppManager的=新ApplicationManager(这一点,getPackageManager());
            appManager.loadApplications(真正的);
            bindApplications();
        }
}
 

解决方案

 静态最后弦乐STATE_USER =用户;
私人字符串MUSER;

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    //检查我们是否再造一个previously破坏实例
    如果(savedInstanceState!= NULL){
        //还原成员的价值,从保存的状态
        MUSER = savedInstanceState.getString(STATE_USER);
    } 其他 {
        //大概初始化成员默认值的新实例
        MUSER =新用户;
    }
}

@覆盖
公共无效的onSaveInstanceState(包savedInstanceState){
    savedInstanceState.putString(STATE_USER,MUSER);
    //总是调用父类,因此它可以保存视图层次状态
    super.onSaveInstanceState(savedInstanceState);
}
 

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

I am trying to save data across orientation changes. As demonstrated in the code below, I use onSaveInstanceState() and onRestoreInstanceState(). I try to get the saved value and I check if it is the correct value in onRestoreInstanceState(). But when I try to use the new value in onCreate(), I don't have the new value but the old one.

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();
        }
}
解决方案
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

这篇关于如何使用的onSaveInstanceState()和onRestoreInstanceState()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:24