共享preferences恢复数据时,应用程序是强制关闭或重启设备 [英] Shared Preferences reset data when app is force closed or device is restarted

查看:220
本文介绍了共享preferences恢复数据时,应用程序是强制关闭或重启设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发的用户名和密码 >共享preferences 。所有的事情都工作正常,对我来说,存储以及检索值。但我发现,当我重新启动设备或应用程序是力封闭储存在的价值共享preferences 复位。当我再次启动我的应用程序,我得到的 键空值共享preferences。在这里,是我在做存储的值:

I'm developing an application in which I'm storing username and password in SharedPreferences. All things are working fine for me, storing as well as retrieving the values. But I discovered that when I restart the device or the app is force closed the value stored in SharedPreferences is reset. And when I again launch my app I get null values in SharedPreferences key. Here, is what I'm doing for storing the values:

SharedPreferences emailLoginSP;

emailLoginSP = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
emailLoginSP.edit().putString("prefEmailId", email_text).commit();
emailLoginSP.edit().putString("prefUserId", userIDToken).commit();
emailLoginSP.edit().putString("prefAccess_token", accessToken).commit();

Intent i = new Intent(LoginWithEmail.this,UserInfoActivity.class);
i.putExtra("acess_token", accessToken);
i.putExtra("user_id", userIDToken);
i.putExtra("emailID", email_text);
startActivity(i);

和,这就是我retriving是:

And, this is how I'm retriving it:

SharedPreferences emailLoginSP = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

loginEmail = emailLoginSP.getString("prefEmailId", null);
loginUserId = emailLoginSP.getString("prefUserId", null);
loginAccessToken = emailLoginSP.getString("prefAccess_token", null);

所有的事情都工作正常至今。 再次声明,我指出我的问题,我得到空值时,我强制关闭或重新启动设备。我们可以永久保存它在应用程序的内存?或者说,我在这里做得不对?

All things are working fine till now. Again I'm stating my problem that I get null values when I force close or restart my device. Can we store it permanently in the app memory? Or, I'm doing something wrong here?

任何帮助将AP preciated。

Any help will be appreciated.

推荐答案

嗯,这就是有趣的。我已经通过这个要求我的应用程序。我有一个登录界面,我想有应用程序留在内屏甚至当应用程序被关闭/销毁/电话/等等登录。我得到它的工作,它现在保持共享preferences 当应用程序被破坏,就像你问。

Hmm thats interesting. I've been through this requirement in my app. I had a login screen and I wanted to have the app stay "logged in" at the internal screen even when the app is closed/destroyed/phone call/etc. I got it working so it now keeps SharedPreferences when the app is destroyed just as you ask.

如果它帮助这里是我的code:

if it helps here is my code:

我有一个 preferences 对象,我用它来保存下一个用户preferences值登录注册。然后我看了preference值中的所有键屏幕 onResume()的方法。

I have a Preferences Object which I use to save preferences values following a user Login or Register. I then read preference values in all the key screen onResume() methods.

登录(例如)后:

SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(activity);
SharedPreferences.Editor editor = app_preferences.edit();
editor.putString("sessionId", application.currentSessionId);
editor.putString("userId", application.currentUserId);
editor.putString("userEmail", application.currentUserEmail);
editor.putString("siteUserId", application.currentSiteUserId);
editor.commit();

在onResume()中的活动: (例如,在你的内部网)

Within onResume() of your Activities: (ie, within your internal screens)

SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(activity);
application.currentSessionId = app_preferences.getString("sessionId", "");
application.currentUserId = app_preferences.getString("userId", "");
application.currentUserEmail = app_preferences.getString("userEmail", "");
application.currentSiteUserId = app_preferences.getString("siteUserId", "");

请注意。我有参加办法全局变量我使用,即 application.currentSessionId ,你可以替换的变量

Note. I have appplication "global" variables i use, ie, application.currentSessionId, you can just substitute your variables

我希望能帮助?尝试类似code,也许你只是没有保存或获取正确的值,因为分享preferences 一定要保留价值,因为你需要

I hope that can help? Try similar code maybe your just not saving or retrieving the values correctly because SharePreferences should definitely retain values as you need

这篇关于共享preferences恢复数据时,应用程序是强制关闭或重启设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆