Sorry my bad English in advance.
I would like to find out how I can clear SharedPreferences when I log out of the application. In my case, the Logout button is on screen 1 (HomePage).
On screen 2, I’m keeping items in a list that generates a Listview with several tiles. When I leave screen 2 to go to another screen, the list items must be kept so that the listview is displayed.This part kinda works.
However, when I log out of the application and log into another account, items from the account I was previously logged into appear. I am aware of the existence of .clear() but I don’t know how it would be implemented given that the list is on one screen and the logout button on another.
- I’ve tried to use clear on the HomePage screen but it didn’t work.
- Tried to create an abstract class involving SharedPreferences but turns out I am not that skillful at managing classes.
2
Answers
I think using Shared Prefs to store a data item that’s common between several users is a bad choice. How would you distinguish them (It’s your problem here)
such common data should be stored in a SQL DB or Baas like firebase.
Shared prefs is generally used to store fast accessible elements like the theme mode or even whether the user is a new user or not.
considering you are just storing id of the user from api and clearing it after logging out.
use shared preference
set data
delete data