【发布时间】:2017-09-30 21:15:00
【问题描述】:
当用户按下注销按钮时,我正在尝试删除另一个活动的共享首选项。首先我将我的变量添加到 SharedPreferences 中,相关代码如下。
SharedPreferences shared_preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = shared_preferences.edit();
int id = Integer.parseInt(cursor.getString(0));
String name = cursor.getString(1);
String surname = cursor.getString(2);
String email = cursor.getString(3);
String username = cursor.getString(4);
String password = cursor.getString(5);
byte[] photograph = cursor.getBlob(6);
String saveThis = Base64.encodeToString(photograph, Base64.DEFAULT);
editor.putInt("id",id);
editor.putString("name",name);
editor.putString("surname",surname);
editor.putString("email",email);
editor.putString("username",username);
editor.putString("password",password);
editor.putString("photograph",saveThis);
editor.commit();
login_screen = new Intent(Login.this, NavigationDrawer.class);
startActivity(login_screen);
现在,我将从 SharedPreferences 中删除所有变量,但是当我尝试以不同的帐户登录时,没有任何反应,并且我的所有数据仍在 SharedPreferences 中。我将如何删除所有变量?这是代码
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.commit();
Intent moveToMain = new Intent(getApplicationContext(), Login.class);
moveToMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(moveToMain);
【问题讨论】:
-
试试这个 editor().clear().apply();
-
什么也没发生 :(
-
您在 log cat 中是否遇到任何错误或其他问题?
-
您似乎在首选项中存储了太多;您是否考虑过使用数据库来存储用户信息?
-
什么都没有发生意味着你如何检查它是否从 prefarances 中删除所有数据?它应该发生
标签: java android android-sharedpreferences