Home > Software engineering >  Getting data from sharedPreference to a specific activity
Getting data from sharedPreference to a specific activity

Time:04-28

Is it possible to get a sharedprefence data from MainActivity_A and fetch it only to MainActivity_B and the other activities like MainActivity_C and MainActivity_D cannot access the data that has been fetched to MainActivity_B?

CodePudding user response:

Well It totally depends on you wheather you wanna get that data or not..once you store data in shared preference with key then just get that data in the specific activity you want Thats It !!

For Example in Activity X you save the Shared Preference value like this

public static final String SHARED_PREFS = "sharedPrefs";

SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS,MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("Key",Value);

Then in Activity A you want to use this value then you can do it like this

SharedPreferences pref;

pref = getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE);
String Value = pref.getString("Key");

In This way the value will be stored in variable String And if you don't want the value in other activity then just don't call it !!

  • Related