用于创建一次性活动的 Android 共享首选项(示例) [英] Android Shared preferences for creating one time activity (example)
本文介绍了用于创建一次性活动的 Android 共享首选项(示例)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有三个活动 A、B 和 C,其中 A 和 B 是表单,并且在填写表单数据并将其保存在数据库(SQLITE)中之后.我正在使用从 A 到 B 然后从 B 到 C 的意图.我想要的是,每次打开我的应用时,我都希望 C 作为我的主屏幕,而不是 A 和 B.
I have three activities A,B and C where A and B are forms and after filling and saving the form data in database(SQLITE). I am using intent from A to B and then B to C.What i want is that every time I open my app I want C as my home screen and not A and B anymore.
我想共享首选项可以解决这个问题,但我找不到一个很好的例子来给我一个起点.任何帮助将不胜感激.
I guess shared preferences would work for this, but I cannot find a good example to give me a starting place. Any help would be appreciated.
推荐答案
在 Preference 中设置值:
// 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.
更多信息:
这篇关于用于创建一次性活动的 Android 共享首选项(示例)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文