android - Restart application after onSharedPreferenceChanged has been called -


how restart application after onsharedpreferencechanged has been called?

activitypreferences.java:

[..] public static class fragmentpreferences extends preferencefragment {         private context context;          @override         public void oncreate(bundle savedinstancestate) {             super.oncreate(savedinstancestate);             context = getactivity();             preferencemanager.setdefaultvalues(getactivity(), r.xml.preferences, false);             addpreferencesfromresource(r.xml.preferences);              final sharedpreferences sp = context.getsharedpreferences("preference_key", context.mode_private);             sharedpreferences.onsharedpreferencechangelistener spchanged = new sharedpreferences.onsharedpreferencechangelistener() {                 @override                 public void onsharedpreferencechanged(sharedpreferences sharedpreferences, string key) {                     context.finish();                     startactivity(new intent(activitypreferences.this, activitylogin.class));                 }             };         }     } 

that's tried, can't call finish().

i want change theme, if listpreference s1_theme has been set light/dark:

activitymain.java:

@override     protected void oncreate(final bundle savedinstancestate) {         super.oncreate(savedinstancestate);         final context context = getapplicationcontext();         final sharedpreferences sp = context.getsharedpreferences("preference_key", context.mode_private);         if (sp.getstring("s1_theme", "-1") == "light"){                 settheme(r.style.apptheme_baselight);         } else {             settheme(r.style.apptheme_basedark);         }         setcontentview(r.layout.activity_main);         [..] 

try code... restart mainactivity...

                    this.finish();                     intent refresh = new intent(this, mainactivity.class);                     startactivity(refresh); 

create method in mainactivity code , call fragment class want restart passing context...


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -