当用户按下注销按钮时,我尝试删除其他活动的共享首选项。首先,我将我的变量添加到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);
答案 0 :(得分:0)
如果您在AndroidManifest.xml中设置了android:allowBackup="true"
,则将其设置为false,因为Android会保留Android 6.0的自动备份,包括以下内容:
有关此内容的更多信息,请访问以下链接: https://developer.android.com/guide/topics/data/autobackup.html
答案 1 :(得分:0)
您可以尝试将SharedPreference实例中的所有变量设置为null,如下所示:
SharedPreferences shared_preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = shared_preferences.edit();
editor.putInt("id",-1);
editor.putString("name",null);
editor.putString("surname",null);
editor.putString("email",null);
editor.putString("username",null);
editor.putString("password",null);
editor.putString("photograph",null);
editor.commit();
这将确保所有变量都已重置
答案 2 :(得分:0)
对不起伙计我认为当我调试来自数据库的变量总是一样的时候我发现了问题。谢谢你的帮助。