0

So what I am trying to do is save the current progress made by the user in Textviews.java through sharedPreferences. E.g. the activity contains several textviews and the user types in them. When the user presses back, it should save the current state of what is typed in the textviews. And once the user presses the exit button on Main.java, an alert dialog comes up asking the user if he would like to save what he typed. If he selects yes, then it saves and the app closes, if not then it doesn't save and the app closes.

So far I have managed to save what the user does when he presses back when inside Textviews.java but I have several questions regarding how to commit the save when the alert dialog comes up when the user press 'yes' on the alert dialog. And also, loading the data when the user goes back to the Textviews activity.

I have a couple of questions regarding sharedPreferences. 1) Throughout the WHOLE app, can it only have one sharedPreference() object to store their data in? Or can there be multiple objects. If multiple objects is allowed, then does 1 activity essentially get one sharePreference object or can one activity have several sahrepreference objects to store their data? 2) How can I access other activities shared preference datas? 3) How can I know if a sharedpreference file exists?

Tarikh Chouhan
  • 425
  • 3
  • 5
  • 15

1 Answers1

1

1) Throughout the WHOLE app, can it only have one sharedPreference() object to store their data in? Or can there be multiple objects. If multiple objects is allowed, then does 1 activity essentially get one sharePreference object or can one activity have several sahrepreference objects to store their data?

There can be multiple Shared Preferences. When you try to get Instance of SharedPreference, you will give it a name.

getSharedPreferences(SHARED_PREFS_FILE_NAME, Context.MODE_PRIVATE);

SHARED_PREFS_FILE_NAME is name of shared preference. if you give different name at different place you will create multiple Shared Preference.

SharedPreference is XML file, different file name, different SP.

2) How can I access other activities shared preference datas? 3) How can I know if a sharedpreference file exists?

When you call this method >> getSharedPreferences(SHARED_PREFS_FILE_NAME, Context.MODE_PRIVATE);

it checks if the sharedPreference file exist, if yes, it will open the existing file, or will create new file.

You can save data from anywhere in the app and can get it back from anywhere, make sure you pass same SharedPreferences name in getSharedPreferences

Here is what Android Document says about [getSharedPreferences][1]

public abstract SharedPreferences getSharedPreferences (String name, int mode)

Retrieve and hold the contents of the preferences file 'name', returning a SharedPreferences through which you can retrieve and modify its values. Only one instance of the SharedPreferences object is returned to any callers for the same name, meaning they will see each other's edits as soon as they are made.

Edit :- To remove Shared Preferences specific values or complete file clear, follow below Reference answer

To remove specific values: SharedPreferences.Editor.remove() followed by a commit()

To remove them all SharedPreferences.Editor.clear() followed by a commit()

Community
  • 1
  • 1
AAnkit
  • 27,299
  • 12
  • 60
  • 71