android - Save App Preferences from PreferenceScreen when pressing back button -
i'm trying learn how develop android apps. followed video tutorial on youtube, , ended adding simple app settings screen application.
however, there's 1 point bothers me: when press button on phone's navigation bar, changed settings aren't applied.
i have tried searching on google, none of solutions found have worked. fact don't yet understand 100% of what's happening on proposed solutions may contribute difficulty on solving 1 problem.
the behavior expect app when press button on navigation bar, changed settings should applied.
for instance, have setting dark background, controlled checkbox. current behavior is: check setting dark background. when press button on navigation bar, setting isn't applied (i have method loads preferences on mainactivity). want happen when press button, dark background applied in case.
from understand, believe overriding onbackpressed
should trick, don't know should executed in order apply settings.
here class , layout of preferencescreen. regarding strings on xml, aren't hard-coded. copied english values here show text should appear on interface.
public class apppreferences extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_note_detail); fragmentmanager fragmentmanager = getfragmentmanager(); fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction(); settingsfragment settingsfragment = new settingsfragment(); fragmenttransaction.add(android.r.id.content, settingsfragment, "settings_fragment"); fragmenttransaction.commit(); } public static class settingsfragment extends preferencefragment { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); addpreferencesfromresource(r.xml.app_preferences); } } } <?xml version="1.0" encoding="utf-8"?> <preferencescreen xmlns:android="http://schemas.android.com/apk/res/android"> <preferencecategory android:title="general"> <edittextpreference android:title="notebook" android:summary="the title used on main action bar." android:key="title" android:defaultvalue="notebook" /> </preferencecategory> <preferencecategory android:title="color"> <checkboxpreference android:title="dark background" android:summary="is main background color dark?" android:key="background_color" android:defaultvalue="false" /> </preferencecategory> </preferencescreen>
you need use
public class apppreferences extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); //setcontentview(r.layout.activity_note_detail); fragmentmanager fragmentmanager = getfragmentmanager(); fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction(); settingsfragment settingsfragment = new settingsfragment(); fragmenttransaction.add(android.r.id.content, settingsfragment, "settings_fragment"); fragmenttransaction.commit(); } public static class settingsfragment extends preferencefragment { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); addpreferencesfromresource(r.xml.app_preferences); preference preference = findpreference("background_color"); preference.setonpreferencechangelistener(new preference.onpreferencechangelistener() { @override public boolean onpreferencechange(preference preference, object newvalue) { //do action here return false; } }); } } }
or other activity:
preferencemanager.setdefaultvalues(this, r.xml.your_setting_xml, false); sharedpreferences settings = preferencemanager.getdefaultsharedpreferences(this); if (settings.getboolean("background_color", true)) { //do action here .........
refer question also, (it has similar use case): checkbox preference , checking if enabled or disable
Comments
Post a Comment