Is access to common functions static?

I have a question that might be more general, but I came across it during android dev:

How can I best use my own common methods? For example, obtaining a shared privilege with a key is always the same code. But if I have to use it in different fragments or actions, I always have to copy the same code:

private void setSharedPrefs(String key, String value) { SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences.Editor editor = settings.edit(); editor.putString(key, value).commit(); } 

Is it a good habit to make this public static in the GlobalUtils class or so? How would you deal with such features?

+4
source share
3 answers

Yes, you can make it static public:

 public static void setSharedPrefs(Context context, String key, String value) { SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences.Editor editor = settings.edit(); editor.putString(key, value).commit(); } 

Be careful in some situations where you can stick to the context after activity has died, which is bad.

A more likely scenario would be to create your class as follows:

 public class MyPrefs { SharedPreferences settings; SharedPreferences.Editor editor; public MyPrefs(Context context){ settings = PreferenceManager.getDefaultSharedPreferences(context); editor = settings.edit(); } public void saveName(String name){ editor.putString("name", name).commit(); } } 

You would be lazy to initialize this class in your class, which extends the application and has a recipient there to receive it, with something like:

  MyPrefs prefs = ((MyAppication) getContext().getApplicationContext()).getMyPrefs(); 

and use it like this:

  prefs.saveName("blundell"); 

EDIT

Example of lazy initialization:

 private MyPrefs prefs; public MyPrefs getMyPrefs(){ if(prefs == null){ prefs = new MyPrefs(this); } return prefs; } 

NB . This is lazy initialization inside the class that extends Application , so this refers to your application context and will live for the duration of your application. If you use an Activity context, you do not want to use lazy initialization. ( So use the application context! )

+1
source

You can definitely create a static class such as GlobalUtils or even a dedicated class for SharedPreferences . You just need to pass the Context method to the method so that you can get the SharedPreferences object. You can accept this as far as you need to go; I have done these exercises countless times. I even have a thread-safe SharedPreferences shell SharedPreferences

EDIT: Just looked at my code, and half of my SharedPreference wrappers SharedPreference static, and the rest are created lazily. That being said, I think that you should do what is convenient for you until the rest of your code requires it to go anyway.

+2
source

I usually write general code inside a static method in another class. So I can call the static method anywhere in the project, each time creating new class objects.

0
source

Source: https://habr.com/ru/post/1413433/


All Articles