我在一项活动中创建了共享首选项数据,是否可以在另一项活动中使用?如果是,如何做到这一点?
4个玩家的名字保存在NameIndex.java中,我想使用MainActivity.java中保存的4个玩家的名字
在 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();
}