android - How to use data stored in sharedpreferences from non-activity class -


I am trying to obtain and update the data stored in the preferences xml file shared from a non-activity class I am But I did not know how to access the data stored in pages shared with a non-activity class in Android. In my active class I can store data in shared speeches and I can also convert my data into activation class. Although I can not withdraw these data from non-active classes My code is below. Thanks ...

  mSharedPrefs = getSharedPreferences ("storage for", 0); MPrefsEditor = mSharedPrefs.edit (); For (int i = 0; i & lt; names.size (); i ++) {mPrefsEditor.putString (indexForFav [i], "0"); } MPrefsEditor.commit (); For (int i = 0; i & lt; names.size (); i ++) {string keyfav = mshadpfs String (indexfour [i], "2"); ValueForFav.add (keyForFav); }   

The key is access to the reference object so if you shared within an object If you want to use referenced references, then you should probably pass a reference object in the class constructor. By doing so you can do this:

  Shared references prefs = context.getSharedPreferences ();    

Comments