1

I have a string and a button as follows:

String message = "Hello World"

button.OnClickListener(......){

   message = "123456789";
}

So here is what I want,

When the app starts the string is "Hello World" but when the user clicks the button it changes to "123456789" and I want the string to change permanently for lifetime.

So when the user restarts the application or reinstall it the string is still "123456789".I think this comes under Shared Preferences.

Please Help,I really need this

Sudheesh R
  • 1,767
  • 3
  • 23
  • 43
Furqan Hussain
  • 135
  • 1
  • 4
  • 14
  • When you reinstall means what uninstall previous apk and install it again? – Abhishek Nov 17 '17 at 06:48
  • **For restarting app** Yes you can use shared preferences. **For Reinstalling** checkout this question, you need to maintain backup for that https://stackoverflow.com/questions/9815363/in-android-is-there-any-way-to-preserve-sharedpreferences-after-an-uninstall – Sara Tirmizi Nov 17 '17 at 06:49
  • This is possible duplicate of [this](https://stackoverflow.com/questions/31108029/how-to-change-the-text-permanently-using-android) This link might help you. – Lalit Verma Nov 17 '17 at 06:53

4 Answers4

4

You can save that string in Shared Preferences, and get it all the from there. If is no value, you can get default value from resources or you can provide your own default String. Note that you can do that for lifetime. If user delete the app or clear cache, your view will display default value, Hello World.

Edit

You can use android:allowBackup="true" from manifest in order to keep old Shared Preference values.

Cătălin Florescu
  • 5,012
  • 1
  • 25
  • 36
  • 1
    So when the user restarts the application or **reinstall it the string is still "123456789"**.I think this comes under Shared Preferences. This needs backup this can not be achieved solely by Shared Preferences! – Sara Tirmizi Nov 17 '17 at 06:51
  • It could use `fullBackupContent` from manifest in order to restore old values. – Cătălin Florescu Nov 17 '17 at 06:53
0

There is a chance data from SharedPreferences is gone when you reinstall especially if you uninstall the app first, same case as local database using SQLite. You can try using database from your local server.

0

To save a String in SharedPreferences :

SharedPreferences.Editor editor = getSharedPreferences("com.package.name", Context.MODE_PRIVATE).edit();
editor.putString("keyName", message);
editor.apply();

To get it back :

SharedPreferences sharedPrefs = getSharedPreferences("com.package.name", Context.MODE_PRIVATE);
message = sharedPrefs.getString("keyName",defaultValue);

When the user modifies it, save it. When the app start, get it with as default value "Hello World" so that while the user hadn't click the button, it will stay as Hello World.

Gueg
  • 127
  • 1
  • 11
0

For Restarting Shared Preference is the best mechanism but for reinstall allowbackup works on API level 23 and above, you can consider to update the value on server once you update in your Shared Preferences for the first time and during reinstall can do a API call check for the same for the particular user.

Tejshree
  • 106
  • 1
  • 3