当用户按下注销按钮时,我正在尝试删除另一个活动中的共享首选项。首先,我将我的变量添加到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);