我想从我的应用程序中删除SharedPreferences,但是没有任何反应。

3

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

1
尝试使用此编辑器().clear().apply(); - Tufan
什么也没有发生 :( - Serdar Yazici
您在log cat中有遇到任何错误或异常吗? - Abdul Waheed
什么都没有发生意味着你如何检查它是否从首选项中删除了所有数据?这应该发生。 - Tufan
另一个想法,也许是因为getDefaultSharedPreferences。请参考https://dev59.com/52gv5IYBdhLWcg3wMN0U#10786739 - Tima
显示剩余4条评论
3个回答

0
如果您在AndroidManifest.xml中设置了android:allowBackup="true",则请将其设置为false,因为Android从Android 6.0开始保留自动备份,包括以下内容:

  • 共享首选项文件。
  • 通过getFilesDir()返回的目录中的文件。
  • 通过getDatabasePath(String)返回的目录中的文件,其中还包括使用SQLiteOpenHelper类创建的文件。
  • 通过getDir(String, int)创建的目录中的文件。
  • 在通过getExternalFilesDir(String)返回的外部存储中的文件。

有关更多信息,请访问以下链接: https://developer.android.com/guide/topics/data/autobackup.html


我认为这不会起作用。通常情况下,当用户卸载应用程序时,allowBackup是有用的,而不是在应用程序中进行操作。将标志设置为true时,它将在重新安装应用程序时恢复值。 - Iulian Popescu

0

对不起,大家。我在调试时发现来自数据库的变量始终相同,这应该就是问题所在了。感谢你们的帮助。


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();

这将确保所有变量已被重置


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接