【问题标题】:android: sharedpreferences possible to use across activities?android:可以跨活动使用的sharedpreferences?
【发布时间】:2012-09-23 07:47:22
【问题描述】:

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

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

【问题讨论】:

  • 你试过什么?在一项或多项活动中使用 sharedpreference 没有任何区别

标签: android android-activity sharedpreferences


【解决方案1】:

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

【讨论】:

  • 我之前尝试过使用一个共享偏好 (SP),即一个键 (slot#) 用于存储 4 个变量(玩家在该特定位置的分数)但失败了。这是因为“SharedPreferences 解决方案就像一张地图。一个 VALUE 到一个 KEY。”我也在stackoverflow.com/questions/12438514/android-sharepreference 上问过这样的问题。也许可以进一步修改编码,以便 1 SP 使用 1 个键获得 4 个分数,但说得简单,不要被卡住,然后定义 4 SP。你能给一些建议吗?总之非常感谢你的建议~
  • @pearmak,我认为您有 2 个选择: - 如果您想继续使用 SharedPreferences,请将字符串用作字符数组或作为每个字段的指示符的纯文本。这是我想做的事情,因为 SharedPreferences 编辑器的 .putString 函数确实只使用 2 个值:要保存的字段的名称和要存储在其中的值。 - 如果您想像数据库一样访问数据,请使用 SQLite。
【解决方案2】:

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

context.getDefaultSharedPreferences()

【讨论】:

    【解决方案3】:

    我就是这样用的

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-14
      相关资源
      最近更新 更多