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 8748439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:31:32+00:00 2026-06-13T12:31:32+00:00

I declared a non-final static SharedPreferences object called PREFS together with the other constants

  • 0

I declared a non-final static SharedPreferences object called PREFS together with the other constants in my Constants.java class:

public static SharedPreferences PREFS = null;

My reason for doing so is that my app is comprised of about 6 different AsyncTasks that keep accessing the same shared prefs file, and I didn’t want to get the SharedPreferences and its Editor every time. On my app’s first run, I make a call to getSharedPreferences() in my MainActivity and store it to PREFS so that it points to an instance before the AsyncTasks are fired, and therefore avoid a NullPointerException.

However, I kept getting NullPointerExceptions from the AsyncTasks anyway. All stack traces point to the line where I make a call to Constants.PREFS to get a value. My theory is that after some time, Android kills my app’s process, so the next time the alarm for any of my AsyncTasks goes off, the value of Constants.PREFS would already be null.

My question is: Should I make a static variable for Context instead? Will its state be persisted even when Android kills off my process? I always pass a reference of Context to my AsyncTasks when the latter are instantiated so they can call on getSystemService(), and that never throws a NullPointerException. However, I don’t see Context extending Serializable from the Android docs.

  • 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-13T12:31:33+00:00Added an answer on June 13, 2026 at 12:31 pm

    Use Application class to share preferences globally across the application.

    public class MyApp extends Application {
        private static Context mAppContext;
    
        @Override
        public void onCreate() {
            super.onCreate();
            mAppContext = getApplicationContext();
        }
    
        public static SharedPreferences getPreferences(){
            return mAppContext.getSharedPreferences("my_app_preferences",MODE_MULTI_PROCESS);
        }
    }
    

    Tell android you want your custom Application class, by mentioning it in the manifest:

    <application android:name=".MyApp" android:label="@string/app_name" android:icon="@drawable/ic_launcher">
    

    Now from anywhere, just call

    MyApp.getPreferences().edit().putBoolean("pref1",true). . . put more stuff . .commit()
    

    MODE_MULTI_PROCESS ensures all AsyncTask threads get synchronized access to same instance of SharedPreferences. Also, commit() is atomic, and will persist changes to the disk.

    The above pattern is also handy for accessing resources from anywhere.

    Update

    It seems that static reference to context causes troubles with new Instant Run feature.

    Hence, the context can be required as a parameter of utility method:

    public static SharedPreferences getPreferences(context){
        return context.getSharedPreferences("my_app_preferences",MODE_MULTI_PROCESS);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Many Java frameworks allow class members used for injection to be declared non-public. For
I created a non-static inner class like this: class Sample { public void sam()
When I try to access the Host object from a non-static method declared in
I have few global methods declared in public class in my ASP.NET web application.
In C++ a stack-allocated object can be declared const : const Class object; after
As texts say, Static methods don't access non-static data, so howcome main() method (declared
Possible Duplicate: Cannot refer to a non-final variable inside an inner class defined in
I recently ran across a class that had the following field declared: private final
Possible Duplicate: Cannot refer to a non-final variable inside an inner class defined in
I am building a dynamic view in a non-activity class, which is called by

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.