Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7960133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:40:50+00:00 2026-06-04T04:40:50+00:00

I have a question which might be more general, but I came across it

  • 0

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

How can I best share own common used methods?
Eg retrieving a shared preference by key is always the same code. But if I have to use it in different Fragments or Activities, 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();
}

It it a good habit to make this a public static in a GlobalUtils class or so?
How would you handle these kind of functions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:40:51+00:00Added an answer on June 4, 2026 at 4:40 am

    Yes you could make it public static:

    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 may hold onto the context after an activity has died, that is bad.

    A more likely scenario your describing could be to create a class like this:

    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 lazy init this class in your class that extends Application and have a getter in there to retrieve it, with something like:

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

    and use it like so:

     prefs.saveName("blundell");
    

    EDIT

    Example of Lazy initialisation:

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

    N.B This is lazy initialization within a class that extends Application therefore this refers to your application context and will live for the duration of your Application. If you where using an Activity context you would not want to use lazy initialisation. (So use the application context!)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question which might sound a bit confusing but i'll try my
I asked a question , title of which might have been misleading so I'm
I might have pretty basic question about regex. I have the following regex, which
I have a question which I am sure is simple but I have slight
I have a question which might be misinterpreted, so let me explain it with
This might be too opinionated a question, but looking for help! I have been
I have a question which is sonewhat more of a design question. I have
This is an odd question. But here goes. I have object X, which gets
This might be more of a design question, but here goes. I'm writing an
I have a question which is slightly similar to this question on stackoverflow std::cin.clear()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.