I am using a contentprovider, and 3 variables on the shared prefs, I’m wondering how to best ‘log the user out’..
I would expect it to truncate the db, and clear/delete shared prefs variables..
Currently I am clearing the shared prefs, and deleting the database, then taking the user back to the login screen.
SharedPreferences app_preferences = PreferenceManager
.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = app_preferences.edit();
// wipe user specific data
editor.remove("authenticated_user_id");
editor.remove("api_key");
editor.remove("last_sync_updates");
editor.commit();
// TODO possibly truncate rather than delete
// the apps database
getApplicationContext().deleteDatabase(
DatabaseConstants.DATABASE_NAME);
// send the user to the login screen
Intent logoutIntent = new Intent(this, SplashActivity.class);
startActivity(logoutIntent);
But it doesn’t seem to clear the database, and I get database access errors randomly on the first transaction after logging out..
How is this typically done?
The Google I/O 2012 application does something similar so you might want to check that out. When the user logs out, the following call to the
ContentResolveris made:This calls the
ScheduleProvider‘s delete method:where
deleteDatabase()is the following private helper method:You can see the exact sequence of events that occurs beginning in
ScheduleProvider.java.