So, let's imagine that we're making Preferences as Settings.
First, we make SettingsActivity
, of course, which is the plain activity we all know of. Its XML must actually be a fragment,
referring to SettingsFragment
(which we'll make; extends PreferenceFragmentCompat
) with android:name
.
Second, we make SettingsFragment with its XML preference xml(<PreferenceScreen and nested preferences in it) in res/xml directory.
And we bond the resource to a Fragment with addPreferencesFromResource
method.
Then, we must not forget to add preference theme(@style/PreferenceThemeOverlay) to styles.xml
, otherwise our app will crash.
Dope fact: any change to the UI in the activity of Settings(of preferences) is automatically saved into SharedPreferences
.