I want to store a string value and need to retrieve even the app is reopened and pass it to another activity to use in that activity. Can somebody guide me here with a sample code/project please
Asked
Active
Viewed 76 times
-2
-
4Use shared preference. see http://developer.android.com/training/basics/data-storage/shared-preferences.html – Rohit5k2 Dec 30 '14 at 20:43
-
1Please search before asking really basic questions. – Simon Dec 30 '14 at 21:10
-
If you could show what you have already tried, it will be easier for someone to help you improve or fix your code. – Lea Cohen Dec 31 '14 at 06:55
1 Answers
3
Use Shared Preferences, presented in the developers android documentation here . More indications here
As explained in the Android documentation:
final String KEY = "stringKey";
String myString = "someStringContent";
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); // this = your Context
SharedPreferences.Editor editor = settings.edit();
editor.putString(KEY, myString);
// Apply the edits! This happens asynchronously
editor.apply();
And later, to retrieve that value when your app is reopened:
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
String myString = settings.getString(KEY, "default value");

Community
- 1
- 1

SummerCode
- 1,403
- 1
- 14
- 28