jaimin
jaimin

Reputation: 563

About Shared Preferences

I'm searching for the best way to store user data like (IP address, Port number, username, Password) for life time of activity.

So I've decided to go use SharedPreferences. While referring this example I saw one answer which is "if user clear the application data from:

setting -> application manager -> your application -> clear data

Then all data saved in shared preferences will get removed

Is this true? Please help me with this. I referred developer guide but its nowhere mentioned that user can clear Shared Preferences data.

Is this a good way to do this? Or there is another better way to get it?

Upvotes: 1

Views: 216

Answers (2)

Xaver Kapeller
Xaver Kapeller

Reputation: 49797

Yes of course if you wipe all the apps data it will be like you uninstalled and reinstalled it. All data in SQLiteDatabases or SharedPreferences will be lost. But that doesn't matter in your case! If you want to save static resources like that then you want to use the app resources. The app resources are static and cannot be wiped or changed at runtime. Try something like this:

Go to the folder res/values in your project and create a file called strings.xml if it doesn't already exist. In it you can define all kinds of Strings like IP adresses or port numbers, login credentials, API keys literally anything like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="username">SomeUserName</string>
    <string name="password">123456</string>

<resources>

And you can read those values from your code like this:

String username = context.getString(R.string.username);
String password = context.getString(R.string.password);

Upvotes: 1

Sam
Sam

Reputation: 4284

yes, when doing clear data all application data including user details will be erased..This would happen if you are storing even in sqlite database... in short, clearing the app data means the life time of app has ended... From this point your app will be starting as a newly installed one..

if you need to keep your data without being destroyed on clearing the app data, you need to save the data in remote database and sync whenever it's not available locally...

Upvotes: 0

Related Questions