-2

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

Mubir
  • 63
  • 1
  • 8

1 Answers1

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