1 回答

TA貢獻1853條經驗 獲得超18個贊
我建議使用 SharedPreferences 來存儲和檢索活動之間的值。
private void storeValue(){
? ? SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
? ? // Store an integer using the Shared Preferences editor
? ? SharedPreferences.Editor editor = prefs.edit();
? ? editor.putInt("myPreferenceValueKey", value); // Store the value with a key to identify it
? ? editor.apply();
}
private void retrieveValue(){
? ? SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
? ? int defaultValue = 0; // This is the value that's returned if the preference doesn't exist
? ? // Retrieve the stored value using the associated key
? ? value = prefs.getInt("myPreferenceValueKey", defaultValue);
}
在您的第一個活動中,只需在增加值之后但在開始下一個活動之前調用retrieveValue()您的onCreate()then 調用。storeValue()
您可以在第二個 Activity 中創建相同的函數來根據需要存儲和檢索值。
添加回答
舉報