Android token - data loss on app cache clear

Viewed 162

I have this token that I am using to call remote rest API
I get this token from the server in the app at runtime (when user install the app)
How/where can I store this token on Android device so it will not go away,
So even if user clear the app cache or clear the app data it will stay?
So local file or db is out of the question..
Thank you

2 Answers

This is not recommended way but if you request external file storage permission from user and store token as file (with encryption) somewhere outside of app storage, you can get it again after clear app storage.

However this is not recommended because when user clear app data app should reset its state, when user uninstall the app the token file is remain in user's storage as a garbage.
Also external files can be deleted by user out side of the app at anytime so you need recovery plan for such situation.

As a conclusion you are encouraged not to create such system. If you need just user identity, check Best practices for unique identifiers to map id to token on server or something like that.

You can't achieve that without writing to external file like mentioned above. What you should do, is save the token to SharedPreferences, check if the token is there every time you launch the app, and ask the server for a new token if it's not there.

Related