I want to remove SharedPreferences from my application. Bt nothing happens
I am trying to remove the shared preferences for a different action when the user presses the logout button. I first added my variables to SharedPreferences, linked code 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 remove all the variables from SharedPreferences, but when I try to log into another account, nothing happens and all my data is still in SharedPreferences. How do I remove 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);
source to share
If you have set android:allowBackup="true"
in your AndroidManifest.xml set it to false as Android keeps an automatic backup from Android 6.0 including the following:
- General settings files.
- Files in the directory returned by getFilesDir ().
- The files in the directory, returned by getDatabasePath (String), which also includes files generated by 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
source to share
What you can try is to set all variables in the SharedPreference instance to 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();
This will ensure that all variables are reset
source to share