Static General Information

I have two methods in activity

private void save(String tag, final boolean isChecked) { SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putBoolean(tag, isChecked); editor.commit(); } private boolean load(String tag) { SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE); return sharedPreferences.getBoolean(tag, false); } 

and I will not statically load the load to get load values ​​from another static method within the same activity. However, when I try to make the loading method static, I certainly get an error due to a non-static link. How can I do this job?

I tried this access to SharedPreferences using static methods with no luck.

Any help would be greatly appreciated!

+3
android static sharedpreferences
Jun 05 '11 at 6:17
source share
1 answer

You can save and load from the Application general settings, and not privileges that are closed to Activity :

 private static boolean load(String tag) { SharedPreferences sharedPreferences = Context.getApplicationContext().getSharedPreferences("namespace", Context.MODE_PRIVATE); return sharedPreferences.getBoolean(tag, false); } 

If you do, make sure that you also save the settings in the same way (using Context.getApplicationContext().getSharedPreferences )

+5
Jun 05 2018-11-11T00:
source share
β€” -



All Articles