2015-02-05 109 views
-1

我的活動時,請記住字符串有一個字符串,它從一個意圖安卓:打開新的活動

String myString = intent.getStringExtra("KEY"); 

獲得本次活動,我開始另一個活動的價值。從新活動中,我想返回到上一個活動,並仍然具有相同的myString值。但是當我打開新的活動時,myString的值被刪除。

我看着onSavedInstancState,但似乎沒有工作。

使myString靜態工作,但我認爲這是不好的編程。

那麼這樣做的最好方法是什麼?

+0

請將其中一個答案標記爲正確,這有助於您解決問題。 – 2015-02-06 10:57:52

回答

0

使用ShareDPreferences存儲您要在以後檢索對象:

SharedPreferences prefs = this.getSharedPreferences(
     "com.example.app", Context.MODE_PRIVATE); 
Editor editor = pref.edit(); 
editor.putString("key", ""KEY"); 
editor.commit(); 

要在以後檢索:

pref.getString("key", null); 
0

您可以使用SharedPreferences

如果你有一個相對較小的鍵值集合,你會想要保存,你應該e SharedPreferences API。