1

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?

Community
  • 1
  • 1
jaimin
  • 563
  • 8
  • 25

2 Answers2

1

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);
Xaver Kapeller
  • 49,491
  • 11
  • 98
  • 86
  • 1
    i think this values are dynamic so how user can store it in string .xml may be this is not a good solution correct me if m wrong – Android is everything for me Jul 30 '14 at 11:39
  • @ Xaver Kapeller , actually i am accepting this values from user so can u tell me how to set values to string – jaimin Jul 30 '14 at 11:44
  • as per my knowledge we cannot setvalue in string.xml file when values are accepting from user cause they are dynamic yes for static above answer will be fine – Android is everything for me Jul 30 '14 at 11:47
  • Yes as I mentioned in my answer the values in the app resources cannot be changed at runtime, they are purely read only. If you are dealing with dynamic values than you need to use `SharedPreferences` or something like that. – Xaver Kapeller Jul 30 '14 at 11:50
0

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...

Sam
  • 4,046
  • 8
  • 31
  • 47