在不同的活动中未检索到SharedPreference值 [英] SharedPreference values not retrieved in different Activities
问题描述
我有三个Activities
.当我的应用程序第一次打开时打开ActivityOne
,我要优先保存一个值,然后使用Intent
进入ActivityThree
并替换布局中的一个Fragment
.
这是用于在ActivityOne中保存价值的代码
I have three Activities
.When my application opens the first time ActivityOne
is opened and I am saving a value in preference and using Intent
I go to ActivityThree
and replace one Fragment
in layout.
This is code for saving value in ActivityOne
SharedPreferences prefs;
SharedPreferences.Editor edit;
prefs=MainActivity.this.getSharedPreferences("myPrefs",MODE_PRIVATE);
edit=prefs.edit();
@Override
public void onResponse(JSONObject response) {
try {
saveToken = response.getString("token");
edit.putString("token", saveToken);
Log.i("Login", saveToken);
edit.apply()
}
catch(JSONException e)
{
}
我从首选项中检索值,并在ActivityThree
的片段中获得正确的值
I retrieve the value from Preference and getting correct value in Fragment of ActivityThree
as
SharedPreferences prefs;
prefs=getActivity().getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
myToken = prefs.getString("token", "empty");
关闭应用程序后,我将从首选项中删除该值.
When application is closed I am removing the value from preference.
AlertDialog.Builder builder = new AlertDialog.Builder(ActivityTwo.this);
builder.setTitle("Exit App");
builder.setMessage("Do you really want to Exit ?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
edit=prefs.edit();
edit.remove("token");
edit.apply();
finish();
}
});
稍后打开我的应用程序时,打开ActivityTwo
.我从首选项中检索一个值,成功登录后,我将一个值保存在首选项中.
这是用于从ActivityTwo中的首选项中检索值的代码
When my application is open later ActivityTwo
is opened.I am retrieving the one value from preference and after successfull login I an saving one value in Preference.
This is code for retrieving value from preference in ActivityTwo
SharedPreferences prefs,prefs1;
SharedPreferences.Editor edit;
prefs1 = ActivityTwo.this.getSharedPreferences("restKey",MODE_PRIVATE);
key=prefs1.getString("key","empty");
在登录后的同一时间,我在首选项"中将一个值另存为
At same time after Login I am saving one value in Preference as
SharedPreferences prefs,prefs1;
SharedPreferences.Editor edit;
prefs = ActivityTwo.this.getSharedPreferences("myPrefs", MODE_PRIVATE);
edit = prefs.edit();
@Override
public void onResponse(JSONObject response) {
try {
saveToken = response.getString("token");
edit.putString("token", saveToken);
edit.apply();
}
catch(JSONEceeption e)
{
}
使用Intent登录后,我打开ActivityThree
并替换一个片段,然后从偏好设置中检索值
After Login using Intent I open ActivityThree
and replace one Fragment and retrieve value from Preference as
SharedPreferences prefs;
prefs=getActivity().getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
myToken = prefs.getString("token", "empty");
但是我没有任何价值吗?
But I am not getting any value in it ?
推荐答案
简单,无需在应用程序中的Preference util类下进行更多工作.
Just simple no need to do more work use below Preference util class in you application.
public class PrefUtils {
//preference file
private static final String DEFAULT_PREFS = "GIVE YOUR APP NAME HERE";
//any numeric getter method will return -1 as default value
private static final int DEFAULT_NUMERIC_VALUE = -1;
//any string getter method will return empty string as default value
private static final String DEFAULT_STRING_VALUE = "";
public static void setString(Context mContext,String key, @Nullable String value) {
SharedPreferences prefs = mContext.getSharedPreferences(DEFAULT_PREFS, Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putString(key, value);
editor.apply();
}
public static String getString(Context mContext,String key) {
SharedPreferences prefs = mContext.getSharedPreferences(DEFAULT_PREFS, Context.MODE_PRIVATE);
return prefs.getString(key, DEFAULT_STRING_VALUE);
}
public static void setBoolean(Context mContext,String key, boolean value) {
SharedPreferences prefs = mContext.getSharedPreferences(DEFAULT_PREFS, Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putBoolean(key, value);
editor.apply();
}
public static boolean getBoolean(Context mContext,String key) {
SharedPreferences prefs = mContext.getSharedPreferences(DEFAULT_PREFS, Context.MODE_PRIVATE);
return prefs.getBoolean(key, false);
}
public static void setLong(Context mContext,String key, long value) {
SharedPreferences prefs = mContext.getSharedPreferences(DEFAULT_PREFS, Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putLong(key, value);
editor.apply();
}
public static long getLong(Context mContext,String key) {
SharedPreferences prefs = mContext.getSharedPreferences(DEFAULT_PREFS, Context.MODE_PRIVATE);
return prefs.getLong(key, DEFAULT_NUMERIC_VALUE);
}
public static void setInteger(Context mContext,String key, int value) {
SharedPreferences prefs = mContext.getSharedPreferences(DEFAULT_PREFS, Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putInt(key, value);
editor.apply();
}
public static int getInteger(Context mContext,String key) {
SharedPreferences prefs = mContext.getSharedPreferences(DEFAULT_PREFS, Context.MODE_PRIVATE);
return prefs.getInt(key, DEFAULT_NUMERIC_VALUE);
}
public static void setFloat(Context mContext,String key, float value) {
SharedPreferences prefs = mContext.getSharedPreferences(DEFAULT_PREFS, Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putFloat(key, value);
editor.apply();
}
public static float getFloat(Context mContext,String key) {
SharedPreferences prefs = mContext.getSharedPreferences(DEFAULT_PREFS, Context.MODE_PRIVATE);
return prefs.getFloat(key, (float) DEFAULT_NUMERIC_VALUE);
}
public static void setObject(Context mContext,String key, @NonNull Object value) {
SharedPreferences prefs = mContext.getSharedPreferences(DEFAULT_PREFS, Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putString(key, mGson.toJson(value));
editor.apply();
}
@Nullable
public static <T> T getObject(Context mContext,String key, Class<T> pojoClass) {
SharedPreferences prefs = mContext.getSharedPreferences(DEFAULT_PREFS, Context.MODE_PRIVATE);
String jsonString = prefs.getString(key, null);
if (jsonString == null) {
return null;
}
return mGson.fromJson(jsonString, pojoClass);
}
public static Map<String, ?> getAll(Context mContext) {
SharedPreferences prefs = mContext.getSharedPreferences(DEFAULT_PREFS, Context.MODE_PRIVATE);
return prefs.getAll();
}
public static void removeKey(Context mContext,String key) {
SharedPreferences prefs = mContext.getSharedPreferences(DEFAULT_PREFS, Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.remove(key);
editor.apply();
}
public static void clearAll(Context mContext) {
SharedPreferences prefs = mContext.getSharedPreferences(DEFAULT_PREFS, Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.clear();
editor.apply();
}
public static boolean setPreferenceArray(Context mContext, String key,
ArrayList<String> array) {
SharedPreferences prefs = mContext.getSharedPreferences(DEFAULT_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(key + "_size", array.size());
for (int i = 0; i < array.size(); i++)
editor.putString(key + "_" + i, array.get(i));
return editor.commit();
}
public static ArrayList<String> getPreferenceArray(Context mContext,String key) {
SharedPreferences prefs = mContext.getSharedPreferences(DEFAULT_PREFS, Context.MODE_PRIVATE);
int size = prefs.getInt(key + "_size", 0);
ArrayList<String> array = new ArrayList<>(size);
for (int i = 0; i < size; i++)
array.add(prefs.getString(key + "_" + i, null));
return array;
}
}
例如您需要做以下事情才能使用上述类.
Ex. You need to do following things to use above class.
(1)将值存储到首选项中.
(1)Store value into preference like.
PrefUtils.setString(context,"Key","Value you need to store");
(2)从首选项中获取值.
(2)Retrive value from the preference.
PrefUtils.getString(context,"Key");
这篇关于在不同的活动中未检索到SharedPreference值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!