如何使用的onSaveInstanceState()和onRestoreInstanceState()? [英] How to use onSaveInstanceState() and onRestoreInstanceState()?

查看:211
本文介绍了如何使用的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();
        }
}
 

解决方案
  

当你的活动被重建后,这是previously破坏,你   可以从捆绑恢复保存的状态,该系统通过   你的活动。两者的onCreate()和onRestoreInstanceState()   回调方法收到包含实例的同捆   状态信息。

     

由于所述的onCreate()方法被调用系统是否是创建   您的活动或新实例再造一个previous,你必须   检查状态是否捆绑前尝试读取它为空。   如果为空,则系统创建的一个新实例   而不是恢复previous一个被摧毁。活动中,

 静态最后弦乐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);
}
 

<一个href="http://developer.android.com/training/basics/activity-lifecycle/recreating.html">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();
        }
}

解决方案

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

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

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