我有三个活动A, B和C,其中A和B是表单,并在数据库(SQLite)中填充和保存表单数据后。我使用意图从A到B,然后从B到C,我想要的是,每次我打开我的应用程序时,我想要C作为我的主屏幕,而不再是A和B。

我想共享偏好可以解决这个问题,但我找不到一个好的例子来给我一个起点。


当前回答

共享偏好是如此容易学习,所以看看这个 关于共享偏好的简单教程

import android.os.Bundle;
import android.preference.PreferenceActivity;

    public class UserSettingActivity extends PreferenceActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

      addPreferencesFromResource(R.xml.settings);

    }
}

其他回答

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.

更多信息:

使用共享首选项

共同的喜好

您还可以看看我过去为这个目的编写的示例项目。我在本地保存一个名称,并在用户请求或应用程序启动时检索它。

但是,此时最好使用commit(而不是apply)来持久化数据。更多信息请点击这里。

如何初始化?

// 0 - for private mode`
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); 

Editor editor = pref.edit();

如何在共享偏好中存储数据?

editor.putString("key_name", "string value"); // Storing string

OR

editor.putInt("key_name", "int value"); //Storing integer

别忘了申请:

editor.apply();

如何从共享首选项检索数据?

pref.getString("key_name", null); // getting String

pref.getInt("key_name", 0); // getting Integer

希望这对你有帮助:)

public class Preferences {

public static final String PREF_NAME = "your preferences name";

@SuppressWarnings("deprecation")
public static final int MODE = Context.MODE_WORLD_WRITEABLE;

public static final String USER_ID = "USER_ID_NEW";
public static final String USER_NAME = "USER_NAME";

public static final String NAME = "NAME";
public static final String EMAIL = "EMAIL";
public static final String PHONE = "PHONE";
public static final String address = "address";

public static void writeBoolean(Context context, String key, boolean value) {
    getEditor(context).putBoolean(key, value).commit();
}

public static boolean readBoolean(Context context, String key,
        boolean defValue) {
    return getPreferences(context).getBoolean(key, defValue);
}

public static void writeInteger(Context context, String key, int value) {
    getEditor(context).putInt(key, value).commit();

}

public static int readInteger(Context context, String key, int defValue) {
    return getPreferences(context).getInt(key, defValue);
}

public static void writeString(Context context, String key, String value) {
    getEditor(context).putString(key, value).commit();

}

public static String readString(Context context, String key, String defValue) {
    return getPreferences(context).getString(key, defValue);
}

public static void writeFloat(Context context, String key, float value) {
    getEditor(context).putFloat(key, value).commit();
}

public static float readFloat(Context context, String key, float defValue) {
    return getPreferences(context).getFloat(key, defValue);
}

public static void writeLong(Context context, String key, long value) {
    getEditor(context).putLong(key, value).commit();
}

public static long readLong(Context context, String key, long defValue) {
    return getPreferences(context).getLong(key, defValue);
}

public static SharedPreferences getPreferences(Context context) {
    return context.getSharedPreferences(PREF_NAME, MODE);
}

public static Editor getEditor(Context context) {
    return getPreferences(context).edit();
}

}

****使用Preferences写入值使用:-****

Preferences.writeString(getApplicationContext(),
                    Preferences.NAME, "dev");

****使用Preferences读取值使用:-****

Preferences.readString(getApplicationContext(), Preferences.NAME,
                    "");