我已經在一個活動創建的sharedpreferences數據,是否有可能在另一個活動中使用?如果是的話,這是如何實現的?android:sharedpreferences可用於跨活動?
的4名球員的名字保存在NameIndex.java,我想用的4名球員的名字保存在MainActivity.java
在NameIndex.java:
private void SaveNamesToFile(String Game1, String P1Name, String P2Name, String P3Name, String P4Name)
// save the new row to the file, then refresh all Buttons
{
// originalScore will be null if we're modifying a slot that is existing already
String originalNameP1 = SavedNameP1.getString(Game1, null); // to return null if this preference does not exist.
String originalNameP2 = SavedNameP2.getString(Game1, null);
String originalNameP3 = SavedNameP3.getString(Game1, null);
String originalNameP4 = SavedNameP4.getString(Game1, null);
// get a SharedPreferences.Editor to store new row data
SharedPreferences.Editor preferencesEditorP1 = SavedNameP1.edit();
SharedPreferences.Editor preferencesEditorP2 = SavedNameP2.edit();
SharedPreferences.Editor preferencesEditorP3 = SavedNameP3.edit();
SharedPreferences.Editor preferencesEditorP4 = SavedNameP4.edit();
preferencesEditorP1.putString(Game1, P1Name);
preferencesEditorP2.putString(Game1, P2Name);
preferencesEditorP3.putString(Game1, P3Name);
preferencesEditorP4.putString(Game1, P4Name);
preferencesEditorP1.apply();
preferencesEditorP2.apply();
preferencesEditorP3.apply();
preferencesEditorP4.apply();
}
你嘗試過什麼?在一個或多個活動中使用共享首選項不會產生任何影響 – njzk2