我有三个活动A, B和C,其中A和B是表单,并在数据库(SQLite)中填充和保存表单数据后。我使用意图从A到B,然后从B到C,我想要的是,每次我打开我的应用程序时,我想要C作为我的主屏幕,而不再是A和B。
我想共享偏好可以解决这个问题,但我找不到一个好的例子来给我一个起点。
我有三个活动A, B和C,其中A和B是表单,并在数据库(SQLite)中填充和保存表单数据后。我使用意图从A到B,然后从B到C,我想要的是,每次我打开我的应用程序时,我想要C作为我的主屏幕,而不再是A和B。
我想共享偏好可以解决这个问题,但我找不到一个好的例子来给我一个起点。
当前回答
您可以创建自定义的SharedPreference类
public class YourPreference {
private static YourPreference yourPreference;
private SharedPreferences sharedPreferences;
public static YourPreference getInstance(Context context) {
if (yourPreference == null) {
yourPreference = new YourPreference(context);
}
return yourPreference;
}
private YourPreference(Context context) {
sharedPreferences = context.getSharedPreferences("YourCustomNamedPreference",Context.MODE_PRIVATE);
}
public void saveData(String key,String value) {
SharedPreferences.Editor prefsEditor = sharedPreferences.edit();
prefsEditor .putString(key, value);
prefsEditor.commit();
}
public String getData(String key) {
if (sharedPreferences!= null) {
return sharedPreferences.getString(key, "");
}
return "";
}
}
你可以像这样获得你的偏好实例:
YourPreference yourPrefrence = YourPreference.getInstance(context);
yourPreference.saveData(YOUR_KEY,YOUR_VALUE);
String value = yourPreference.getData(YOUR_KEY);
其他回答
SharedPreferences mPref;
SharedPreferences.Editor editor;
public SharedPrefrences(Context mContext) {
mPref = mContext.getSharedPreferences(Constant.SharedPreferences, Context.MODE_PRIVATE);
editor=mPref.edit();
}
public void setLocation(String latitude, String longitude) {
SharedPreferences.Editor editor = mPref.edit();
editor.putString("latitude", latitude);
editor.putString("longitude", longitude);
editor.apply();
}
public String getLatitude() {
return mPref.getString("latitude", "");
}
public String getLongitude() {
return mPref.getString("longitude", "");
}
public void setGCM(String gcm_id, String device_id) {
editor.putString("gcm_id", gcm_id);
editor.putString("device_id", device_id);
editor.apply();
}
public String getGCMId() {
return mPref.getString("gcm_id", "");
}
public String getDeviceId() {
return mPref.getString("device_id", "");
}
public void setUserData(User user){
Gson gson = new Gson();
String json = gson.toJson(user);
editor.putString("user", json);
editor.apply();
}
public User getUserData(){
Gson gson = new Gson();
String json = mPref.getString("user", "");
User user = gson.fromJson(json, User.class);
return user;
}
public void setSocialMediaStatus(SocialMedialStatus status){
Gson gson = new Gson();
String json = gson.toJson(status);
editor.putString("status", json);
editor.apply();
}
public SocialMedialStatus getSocialMediaStatus(){
Gson gson = new Gson();
String json = mPref.getString("status", "");
SocialMedialStatus status = gson.fromJson(json, SocialMedialStatus.class);
return status;
}
Initialise here..
SharedPreferences msharedpref = getSharedPreferences("msh",
MODE_PRIVATE);
Editor editor = msharedpref.edit();
store data...
editor.putString("id",uida); //uida is your string to be stored
editor.commit();
finish();
fetch...
SharedPreferences prefs = this.getSharedPreferences("msh", Context.MODE_PRIVATE);
uida = prefs.getString("id", "");
在“偏好”中设置值:
// MY_PREFS_NAME - a static String variable like:
//public static final String MY_PREFS_NAME = "MyPrefsFile";
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("name", "Elena");
editor.putInt("idName", 12);
editor.apply();
从首选项检索数据:
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
int idName = prefs.getInt("idName", 0); //0 is the default value.
更多信息:
使用共享首选项
共同的喜好
您可以创建自定义的SharedPreference类
public class YourPreference {
private static YourPreference yourPreference;
private SharedPreferences sharedPreferences;
public static YourPreference getInstance(Context context) {
if (yourPreference == null) {
yourPreference = new YourPreference(context);
}
return yourPreference;
}
private YourPreference(Context context) {
sharedPreferences = context.getSharedPreferences("YourCustomNamedPreference",Context.MODE_PRIVATE);
}
public void saveData(String key,String value) {
SharedPreferences.Editor prefsEditor = sharedPreferences.edit();
prefsEditor .putString(key, value);
prefsEditor.commit();
}
public String getData(String key) {
if (sharedPreferences!= null) {
return sharedPreferences.getString(key, "");
}
return "";
}
}
你可以像这样获得你的偏好实例:
YourPreference yourPrefrence = YourPreference.getInstance(context);
yourPreference.saveData(YOUR_KEY,YOUR_VALUE);
String value = yourPreference.getData(YOUR_KEY);
// Create object of SharedPreferences.
SharedPreferences sharedPref = getSharedPreferences("mypref", 0);
//now get Editor
SharedPreferences.Editor editor = sharedPref.edit();
//put your value
editor.putString("name", required_Text);
//commits your edits
editor.commit();
// Its used to retrieve data
SharedPreferences sharedPref = getSharedPreferences("mypref", 0);
String name = sharedPref.getString("name", "");
if (name.equalsIgnoreCase("required_Text")) {
Log.v("Matched","Required Text Matched");
} else {
Log.v("Not Matched","Required Text Not Matched");
}