2017-03-03 68 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)"); 
}