3

I'm trying to delete shared preferences on another activity when the user press logout button. First I added my variables to the SharedPreferences, related code is below.

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

Now, I will delete the all variables from the SharedPreferences but when I trying to login in different account nothing happens and all my data is still in SharedPreferences. How will I delete all variables ? Here is the code

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);
Suraj Makhija
  • 1,376
  • 8
  • 16

3 Answers3

0

If you have set android:allowBackup="true" in your AndroidManifest.xml, then set it to false as Android keeps an auto backup from Android 6.0 including the following :

  • Shared preferences files.
  • Files in the directory returned by getFilesDir().
  • Files in the directory returned by getDatabasePath(String), which also includes files created with the SQLiteOpenHelper class.
  • Files in directories created with getDir(String, int).
  • Files on external storage in the directory returned by getExternalFilesDir(String).

For more information on this, visit the following link : https://developer.android.com/guide/topics/data/autobackup.html

Suraj Makhija
  • 1,376
  • 8
  • 16
  • I don't think that this will work. In general, the `allowBackup` is useful when the user uninstalls the app, not in a journey through the app. By setting the flag to true, it will restore the values when the app is reinstalled. – Iulian Popescu May 03 '17 at 07:21
0

What you can try is to set all the variables in your SharedPreference instance to null like this:

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

This will ensure that all the variables have been reset

Arjun
  • 1
0

Sorry guys I think I found the problem when I debugging the variables comes from the database are always same. Thanks for help.