简体   繁体   English

android:如何保存我的用户界面并在重启后找回它?

[英]android: how to save my user interface and get it back after reboot?

Sorry I am totally new to Android 抱歉,我是Android的新手

My application has a very simple GUI (2 checkboxes, 3 textiputs). 我的应用程序具有非常简单的GUI(2个复选框,3个文本输入)。 I want to save the value of these widgets (checkboxes, textinputs), so after I reboot my andorid they keep their values. 我想保存这些小部件(复选框,文本输入)的值,因此在重新启动Andorid之后,它们将保留其值。

I guess I can use some local stoarge or db storage, but I am not fluent enough to pick up the fastest/better solution to do that? 我想我可以使用一些本地存储或数据库存储,但是我不够流利,无法选择最快/更好的解决方案来做到这一点?

Regards 问候

Here is the documentation for storing data in Android 这是用于在Android中存储数据的文档

Since you are working with a small amount of data to be saved, I'd recommend option #1: Saving Key-Value Sets aka SharedPreferences 由于您正在处理少量要保存的数据,因此我建议您选择选项1: 保存键值集 (也称为SharedPreferences

You can do it as follows 您可以按照以下步骤进行操作

Writing to Shared Preferences 写入共享首选项

SharedPreferences shp = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = shp.edit();
editor.putString("YourPreferenceName", yourVariable);
editor.commit();

Reading from Shared Preferences 从共享首选项中读取

SharedPreferences shp = getActivity().getPreferences(Context.MODE_PRIVATE);
String yourVariable= shp.getString"YourPreferenceName", "default");

Store the values in SharedPreferences . 将值存储在SharedPreferences Alternatively, read the page on Settings screens (which use SharedPreference s behind the scenes). 或者,阅读“ 设置”屏幕上的页面 (在后台使用SharedPreference )。

You can use PreferenceFragment which can automagically store those values in the SharedPreferences . 您可以使用PreferenceFragment来自动将这些值存储在SharedPreferences中 Here is a good tutorial . 这是一个很好的教程 If thats all you need to store locally, using PreferenceFragment will save you time coding saving/retrieving/displaying those values from SharedPreferences yourself. 如果这就是您需要在本地存储的全部内容,则使用PreferenceFragment可以节省您自己编写,保存/检索/显示来自SharedPreferences值的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即使Android重新启动后也保存/获取列表 - Save/Fetch List even after Android reboot 如何在重启后在android中自动启动我的应用程序? - how can I autostart my app in android after reboot? Android:如何单一更新位置并在重启后无法获取位置 - Android: how to singleUpdate location & can't get location after reboot 设备重启后如何获取小部件尺寸(对于Android 4.4.4) - How to get widget size after device reboot (For Android 4.4.4) Android使用共享的首选项和Dispatcher活动,以返回到上一个活动(重启手机后) - Android using shared preference and Dispatcher activity, to get back to the last activity (after reboot the phone) 如何保存Android通知并在重新启动时将其还原 - How to save android notifications and restore them on reboot 如何在Android中保存并获取自定义对象? - How to save and get back a custom object in Android? 手机重启后Android获取位置为空 - Android get location is null after phone reboot 重启后,Android在接收器中获得更多 - Android get extra in receiver after reboot 手机重启后Android无法获取位置 - Android failing to get location after a phone reboot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM