Java 我想从我的应用程序中删除SharedReferences,但什么都没有发生
当用户按下注销按钮时,我试图删除另一个活动的共享首选项。首先,我将变量添加到SharedReferences中,相关代码如下Java 我想从我的应用程序中删除SharedReferences,但什么都没有发生,java,android,android-sharedpreferences,Java,Android,Android Sharedpreferences,当用户按下注销按钮时,我试图删除另一个活动的共享首选项。首先,我将变量添加到SharedReferences中,相关代码如下 SharedPreferences shared_preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); SharedPreferences.Editor editor = shared_prefere
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);
现在,我将从SharedReferences中删除所有变量,但是当我尝试登录到不同的帐户时,什么都没有发生,我的所有数据仍然在SharedReferences中。如何删除所有变量?这是密码
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);
如果您在AndroidManifest.xml中设置了
android:allowBackup=“true”
,则将其设置为false,因为android会从android 6.0进行自动备份,包括以下内容:
- 共享首选项文件
- GetFileDir()返回的目录中的文件
- getDatabasePath(字符串)返回的目录中的文件 包括使用SQLiteOpenHelper类创建的文件
- 使用getDir(字符串,int)创建的目录中的文件
- 返回的目录中的外部存储上的文件 getExternalFilesDir(字符串)
您可以尝试将SharedReference实例中的所有变量设置为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();
这将确保所有变量都已重置抱歉,伙计们,我想我在调试数据库中的变量时发现了问题,这些变量总是相同的。谢谢您的帮助。尝试此编辑器().clear().apply();什么也没发生:(您在log cat中是否有任何错误或其他信息?您似乎在首选项中存储了太多信息;您是否考虑过使用数据库存储用户信息?什么都没有发生意味着您如何检查它是否从首选项中删除了所有数据?应该会发生这种情况。我认为这不会起作用。通常,使用
allowBackup
)ful当用户卸载应用程序时,而不是在通过应用程序的过程中。通过将标志设置为true,它将在重新安装应用程序时恢复值。