【问题标题】:I want to delete SharedPreferences from my application bt nothing happens我想从我的应用程序中删除 SharedPreferences 没有任何反应
【发布时间】: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


【解决方案1】:

如果您在 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,它将在重新安装应用程序时恢复这些值。
【解决方案2】:

您可以尝试将 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();

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

【讨论】:

    【解决方案3】:

    对不起,我想我在调试来自数据库的变量时发现问题总是相同的。感谢您的帮助。

    【讨论】:

      猜你喜欢
      • 2017-04-22
      • 2021-12-31
      • 2013-08-23
      • 2020-09-23
      • 1970-01-01
      • 2018-05-19
      • 1970-01-01
      • 2015-07-05
      • 1970-01-01
      相关资源
      最近更新 更多