Hello i m GOLD.
this time i m going to make a preference screen in my app, and send a data to another activity.
1. add implementation
first, we need to add implementation about preference in build.gradle(module: app)
then we can use it. cool.
2. Create Options Menu
in MainActivity(or some activity) i m going to override onCreateOptionsMenu and onOptionsItemSelected.
in onCreateOptionsMenu, i m going to use inflate for Instantiates a layout XML file into its corresponding view objects. maybe you can see the red line. its okay. i gonna make menu folder and main_menu.xml file. then it will be gone. res -> (mouse right click) -> New resources Directory -> (type and name)menu -> ok and create main_menu(or what you want).xml file in menu folder.
my xml file is so simple just one item "Settings". the important thing is id. you should write it for use it.
(now if you play this app, you can see the options menu in your app right top side.)
when our settings menu button click, then move to another activity.
override onOptionsItemSelected. write your item id. i set it "settings". so it is R.id.settings ->. in when item.itemId is corrected, then go to the preSetting function.
preSetting() function is just send intent to SettingsActivity. and start it.
so if you click menu option settings button, then go to the SettingsActivity(or what you made).
3. make a preference xml file
res -> Android Resources Directory -> create xml folder and preference.xml file.
you can divide preferences into PreferenceCategory.
ListPreference is when you click it, create a dialog. so we need to create dialog entries and entryValues xml file for the dialog.
res -> values -> create a array.xml
you can make an array what you want. the font_list is on the dialog. the font_values is clicked value in regular order.
4. create a fragment
i gonna make a fragment class. this fragment will show a preference layout what i made.
so override onCreatePreferences function and put my xml into addPreferencesFromResource. done.
5. intended Activity
second, in intended activity(in my case SettingsActivity) xml file has a fragment only.
as you can see, fragment in activity_settings.xml refer my SettingsFragment. it is important. now play it.
6. send data to main activity
if you want to send some data(like entry in dialog what you clicked) to mainActivity(or other activity), you can use PreferenceManager. it can set values to key-value. so i create value with PreferenceManager.Editor() for send data to another activity.
in getString, Key values are taken from the xml file we made. put the corresponding value in the editor. and commit.
now back to the MainActivity. we need to get the editor value.
so i use PreferenceManager again in MainActivity and getString. in getString, Key value is editor.putString's key value, not shared.getString's key value in SettingsActivity. it's a bit confuse. and you should use this function in onResume fun(or something in your code) in MainActivity.
now play it.
i clicked the comic value, and go back to the MainActivity. you can see the Toast message in MainActivity.
this is GOLD
thank you.
*my english is not good sorry about it.
reference : stackoverflow.com/questions/6186123/android-how-do-i-get-sharedpreferences-from-another-activity