android - how to set the context of another class from different class? -
i have main activity , settings activity. settings activity class fired via intent when clicked on menu item settings. now, want make sure default values set in preferences inside oncreate mainactivity call following code. error mainactivity context not taken setdefaultvalues method. think needs context of settings activity. how can pass in correct context mainactivity. in other words should put instead of main activity oncreate method.
preferencemanager.setdefaultvalues(this, r.xml.preferences, false);
update:
my main activity looks follows: here important part setdefaultvalues getting error on part. "this" not acceptable there. when set default values inside settingsfragment or settingsactivity class works fine looks need pass context of settingsactivity , not mainactivity. please help! thanks!
public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); preferencemanager.setdefaultvalues(this, r.xml.preferences, false); }
i have different activity settings
public class settingsactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_settings); getfragmentmanager().begintransaction().add(r.id.fragment_container, new settingsfragment()).commit(); } public static class settingsfragment extends preferencefragment implements sharedpreferences.onsharedpreferencechangelistener{
this error getting
i appreciate help! thanks!
use application context , save values prefereces different name.
Comments
Post a Comment