2017-03-03 65 views
1

为什么在“savedInstanceState”我用 会发生以下问题? 当程序第一次运行时收到正确的消息。恢复savedInstanceState不匹配存储

但是,当我选择视频画廊的视频。 在'savedInstanceState' “测试”来保存它 但是当恢复是 等条件'测试' 你不能运行吗? 和命令

'Else if (savedInstanceState.getString (" key ")! ="test")' 

已运行,而 'savedInstanceState.getString( “钥匙”)' 量 '测试'?

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     if (savedInstanceState != null) { 

      if (savedInstanceState.getString("key")=="test"){ 

       TextView txtarr_1=(TextView) findViewById(R.id.test_array_1); 
       txtarr_1.setText("if(savedInstanceState.getString(key)==test){" + savedInstanceState.getString("key")+"}"); 

      } 
      else if (savedInstanceState.getString("key")!="test") { 
       TextView txtarr_3=(TextView) findViewById(R.id.test_array_3); 
       txtarr_3.setText(savedInstanceState.getString("key")); 

      }  

     }else{ 
     TextView txtarr_5=(TextView) findViewById(R.id.test_array_5); 
     txtarr_5.setText("elseif (savedInstanceState != null)"); 
     }} 

    @Override 
    public void onSaveInstanceState(Bundle savedInstanceState) { 
     savedInstanceState.putString("key", "test"); 

     super.onSaveInstanceState(savedInstanceState); 
    } 

回答

0

我改变代码::而不是存储的字符串 - >存储整数

@SuppressLint("NewApi") 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 

    if (savedInstanceState != null) { 

    int val=savedInstanceState.getInt("key"); 

      if (val==123){ 

    TextView txtarr_1=(TextView) findViewById(R.id.test_array_1); 

txtarr_1.setText("if == 123{" + savedInstanceState.getInt("key")+"}"); 


    } 
    else if (val!=123) { 
    TextView txtarr_3=(TextView) findViewById(R.id.test_array_3); 
txtarr_3.setText(savedInstanceState.getString("key")); 
    }  

    }else{ 
TextView txtarr_5=(TextView) findViewById(R.id.test_array_5); 
txtarr_5.setText("elseif (savedInstanceState != null)"); 
    }} 

@Override 
public void onSaveInstanceState(Bundle savedInstanceState) { 
savedInstanceState.putInt("key", putInt); 

    super.onSaveInstanceState(savedInstanceState); 
} 

在这种情况下,这个问题是可以解决的 这意味着: 检测到我店123和订单等于123进行 但在通话,如果我储存123形成的字符串 运行的订单不等于123执行

一个新的活动要存储的形式 并在还原存储的值是不同的 但是,当我显示完全相同的文字?????? 为什么?????

0

我发现我的错误

为IF字符串必须使用等于,不==

if (savedInstanceState != null) { 
    String str =savedInstanceState.getString("key"); 
    if (str.equals("test")){ 

     TextView txtarr_1=(TextView) findViewById(R.id.test_array_1); 
     txtarr_1.setText("if == test{" + str+"}"); 

    } 
    else if (!str.equals("test")){ 
     TextView txtarr_3=(TextView) findViewById(R.id.test_array_3); 
     txtarr_3.setText("if != test{" + str+"}"); 

    }  

}else{ 
TextView txtarr_5=(TextView) findViewById(R.id.test_array_5); 
txtarr_5.setText("elseif (savedInstanceState != null)"); 
}