android:sharedpreferences可以跨活动使用吗?

时间:2012-10-02 12:50:09

标签: android android-activity sharedpreferences

我在一个活动中创建了一个共享偏好数据,是否可以在另一个活动中使用?如果是的话,怎么能实现呢?

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

3 个答案:

答案 0 :(得分:0)

是的,他们可以跨活动分享。最简单的方法就是使用:

context.getDefaultSharedPreferences()

答案 1 :(得分: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;
        }
    }

答案 2 :(得分:0)

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