title: singleton shared preferences issue
body: <p>This just an example of my application. </p>
  
  <p>I have created a singleton shared preferences which used a custom Shared preference manager class to edit the shared preferences data values:</p>
  
  <pre><code>public class MySharedPrefManager {
      private static MySharedPrefManager instance= null;
      private static SharedPreferences SharedPref;
      private static Editor SPEditor;
  
      private  MySharedPrefManager () {
      }
      public static MySharedPrefManager getInstance(){
          if(instance==null)
              instance= new MySharedPrefManager ();
          return instance;
      }
  
      public void setSharedPreferences(Context context){
          SharedPref= context.getSharedPreferences("MySharedPref", Context.MODE_PRIVATE);
          SPEditor= SPEditor.edit();
      }
      public void setAdmin(boolean pAdmin) {
          SharedPrefManager.editBoolean("isAdmin", pAdmin, SPEditor);
      }
      public boolean isAdmin() {          
          return SharedPref.getBoolean("isAdmin", false);
      }
  }
  </code></pre>
  
  <p>Shared preference manager:</p>
  
  <pre><code>public class SharedPrefManager {
      public static void editString(String key, String value, Editor pEditor){
          pEditor.putString(key, value);
          pEditor.commit();
      }
      public static void editBoolean(String key, boolean value, Editor pEditor){
          pEditor.putBoolean(key, value);
          pEditor.commit();
      }
  </code></pre>
  
  <p>I have lots of activities which goes like activities: A->B->C->D->E->F->G</p>
  
  <p>Activity A, which is the start-up activity, i get the instance of MySharedPrefManager and set the SharedPreferences also:</p>
  
  <pre><code>public class ActivityA extends Activity{
      private MySharedPrefManager myPref;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.initialization);
  
          myPref= MySharedPrefManager.getInstance();
          // I am setting the SharedPreference Context with getApplicationContext(),
          // as it is singleton, and I am using it through out my application
          myPref.setSharedPreferences(getApplicationContext());
          // other stuff...
      }
  
      private void changeData(){
          myPref.setAdmin(true);
      }
  
      private void check(){
          if(myPref.isAdmin()){
              // do- something
          }
      }
  }
  </code></pre>
  
  <p>ActivityD :</p>
  
  <pre><code>public class ActivityD extends Activity{
  
      private MySharedPrefManager myPref;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.initialization);
          // Here i called only the instance not the setSharedPreferences.
          myPref= MySharedPrefManager.getInstance();
  
          // other stuff...
      }
      private void changeData(){
          myPref.setAdmin(true);
      }
      private void check(){
          if(myPref.isAdmin()){
              // do- something
          }
      }
  }
  </code></pre>
  
  <p>Now, the issue I am facing right now is that, some times I get <strong>Null pointer exception in myPref.isAdmin()</strong> when I call it both in Activity A and D. But most of the time it works.</p>
  
  <p>Also do I need to set the <strong>SharedPreferences (call the setSharedPreferences() method) in each of the activities?</strong> I don't feel the need to set is as it is singleton class.</p>
  
  <p>I repeat, I am setting the SharedPreference Context with getApplicationContext(),as it is singleton and I am using it through out my application.</p>
  
  <p><strong>Edited:</strong> There are 3-4 shared preferences that I am using with the same structure.. With the similar problem. Using this <strong>PreferenceManager.getDefaultSharedPreferences(context);</strong>, I will be able to use only ONE SharedPreference which is the default SharedPreference.</p>