Android 来自不同活动的 SharedPreferences

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11316560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:54:48  来源:igfitidea点击:

SharedPreferences from different activity

android

提问by user1390816

I load from activity A the SharedPreferences in following way:

我通过以下方式从活动 A 加载 SharedPreferences:

private void SavePreferences(String key, String value){
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(key, value);
    editor.commit();
}

At activity B I want to load the SharedPreferences. Following was a NullPointerException:

在活动 BI 想要加载 SharedPreferences。以下是 NullPointerException:

private void LoadPreferences(){   
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    data = sharedPreferences.getString("name", "08:00") ;
}

If I try following, I get this compilation error: "No enclosing instance of the type A is accessible in scope"

如果我尝试以下操作,则会收到此编译错误:“在范围内无法访问类型 A 的封闭实例”

private void LoadPreferences(){   
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(A.this);
    data = sharedPreferences.getString("name", "08:00") ;
}

How can I access the data?

如何访问数据?

回答by ρяσ?ρ?я K

use getApplicationContext()instead of thisin both Activities as:

在两个活动中使用getApplicationContext()代替this

In activity A the SharedPreferences in following way:

在活动 A 中, SharedPreferences 以下列方式:

 private void SavePreferences(String key, String value){
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.commit();
        Intent sd=new Intent(this,Secongtess.class);
        startActivity(sd);
       }

and in Activity B get Value as:

并在活动 B 中获得价值为:

 private void LoadPreferences(){   
       SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
     String  data = sharedPreferences.getString("name", "08:00") ;
     Toast.makeText(this,data, Toast.LENGTH_LONG).show();
   }

because as doc says:

因为正如医生所说:

getDefaultSharedPreferences(Context context):

getDefaultSharedPreferences(上下文上下文)

Gets a SharedPreferences instance that points to the default file that is used by the preference framework in the given context.

获取一个 SharedPreferences 实例,该实例指向给定上下文中首选项框架使用的默认文件。

回答by Goofy

To store values in shared preferences:

要将值存储在共享首选项中:

SharedPreferences preferences==PreferenceManager.getDefaultSharedPreferences(this);
  SharedPreferences.Editor editor=preferences.edit();
  editor.putString("Name","Harneet");
  editor.commit();

To retrieve values from shared preferences:

要从共享首选项中检索值:

SharedPreferences preferences==PreferenceManager.getDefaultSharedPreferences(this);
  String name=preferences.getString("Name","");
  if(!name.equalsIgnoreCase(""))
  {
    name=name+"  Sethi";  /* Edit the value here*/
  }

To edit data from sharedpreference

从共享首选项编辑数据

SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
 editor.putString("text", mSaved.getText().toString());
 editor.putInt("selection-start", mSaved.getSelectionStart());
 editor.putInt("selection-end", mSaved.getSelectionEnd());
 editor.commit();

To retrieve data from shared preference

从共享首选项中检索数据

SharedPreferences prefs = getPreferences(MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
if (restoredText != null) 
{
  //mSaved.setText(restoredText, TextView.BufferType.EDITABLE);
  int selectionStart = prefs.getInt("selection-start", -1);
  int selectionEnd = prefs.getInt("selection-end", -1);
  /*if (selectionStart != -1 && selectionEnd != -1)
  {
     mSaved.setSelection(selectionStart, selectionEnd);
  }*/
}