在方向更改时从 savedInstancestate 获取布尔值

Get boolean from savedInstancestate on orientatioin change

我想在 Bundle 中保存一些布尔变量,即 savedInstaceState 那些我可以在方向改变后使用的变量。

我试过了,但 savedInstanceState 结果总是 null

if(savedInstanceState==null){
 savedInstanceState=new Bundle();
 savedInstanceState.putBoolean("isLogoLoaded",true);
 }else{
 savedInstanceState.putBoolean("isLogoLoaded",true);
}  

请提供更好的方法谢谢。

您需要使用

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putBoolean("isLogoLoaded",true);
}

public void onRestoreInstanceState(Bundle savedInstanceState) {
          super.onRestoreInstanceState(savedInstanceState);
          logo = savedInstanceState.getBoolean("isLogoLoaded"); 
}

您可以使用以下代码保存要恢复的值 -

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

希望这对您有所帮助:)

}

您调用该逻辑的方法是什么?

我猜它在 onCreate() 中,onCreate 是您加载 savedInstanceState 的地方,而不是您保存数据的地方。 (也可以在onRestoreInstanceState中加载)

您想将数据保存在 public void onSaveInstanceState(Bundle savedInstanceState) {

 @Override
 public void onSaveInstanceState(Bundle savedInstanceState) {
     super.onSaveInstanceState(savedInstanceState);
     savedInstanceState.putBoolean("isLogoLoaded", true);
 }

从这里开始http://developer.android.com/training/basics/activity-lifecycle/recreating.html