Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android token - data loss on app cache clear

Tags:

android

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

like image 219
JavaSheriff Avatar asked Aug 30 '26 20:08

JavaSheriff


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.

like image 198
ytRino Avatar answered Sep 01 '26 11:09

ytRino


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.

like image 40
Boba0514 Avatar answered Sep 01 '26 11:09

Boba0514



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!