2

我在一项活动中创建了共享首选项数据,是否可以在另一项活动中使用?如果是,如何做到这一点?

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();  
   }
4

3 回答 3

0

是的,它们可以跨活动共享。最简单的方法是使用:

context.getDefaultSharedPreferences()
于 2012-10-02T12:53:12.117 回答
0

我正在这样使用它

     public class SharedPreferencesHelper {

        SharedPreferences myPrefs;
        SharedPreferences.Editor prefsEditor;

        private static SharedPreferencesHelper instance = null;

        public static synchronized SharedPreferencesHelper getInstance() {
            if (instance == null) {
                instance = new SharedPreferencesHelper();
            }

            return instance;
        }



private SharedPreferencesHelper() {
myPrefs  = MyApplication.getInstanse().getApplicationContext().getSharedPreferences("prefs", Context.MODE_WORLD_READABLE);
            prefsEditor = myPrefs.edit();
    }

        public void putValueForKey(String key, String value) {
            prefsEditor.putString(key, value);
            prefsEditor.commit();
        }

    }


    public class MyApplication extends Application {

        private static MyApplication instance;

        @Override
        public void onCreate() {
            super.onCreate();
            instance = this; 
        }

        public static MyApplication getInstanse(){
            if(instance ==null){
                throw new IllegalStateException("Application not created yet!");
            }
            return instance;
        }
    }
于 2012-10-02T12:54:24.417 回答
0

我在活动之间使用了一个 SharedPreferences 文件,但我所做的是使用在两个活动内的不同私有变量中声明的相同文件名。您可以在以下链接中查看我的代码。我不明白的是,为什么您只使用 4 个 SharedReferences 文件作为播放器的名称,而不是仅使用 1 个文件中的所有名称。这是可能的,因为我用它来保存超过 2 个变量。

于 2012-10-02T12:56:58.377 回答