title: Singleton in Android
body: <p>I have followed this link and successfully made singleton class in Android.
  <a href="http://www.devahead.com/blog/2011/06/extending-the-android-application-class-and-dealing-with-singleton/" rel="noreferrer">http://www.devahead.com/blog/2011/06/extending-the-android-application-class-and-dealing-with-singleton/</a></p>
  
  <p>Problem is that i want a single object. like i have Activity A and Activity B. In Activity A I access the object from Singleton <code>class</code>. I use the object and made some changes to it.</p>
  
  <p>When I move to Activity B and access the object from Singleton Class it gave me the initialized object and does not keep the changes which i have made in Activity A.
  Is there any other way to save the changing?
  Please help me Experts.
  This is <code>MainActivity</code></p>
  
  <pre><code>public class MainActivity extends Activity {
      protected MyApplication app;        
      private OnClickListener btn2=new OnClickListener() {    
          @Override
          public void onClick(View arg0) {
              Intent intent=new Intent(MainActivity.this,NextActivity.class);
              startActivity(intent);              
          }
      };
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          //Get the application instance
          app = (MyApplication)getApplication();
  
          // Call a custom application method
          app.customAppMethod();
  
          // Call a custom method in MySingleton
          Singleton.getInstance().customSingletonMethod();
  
          Singleton.getInstance();
          // Read the value of a variable in MySingleton
          String singletonVar = Singleton.customVar;
  
          Log.d("Test",singletonVar);
          singletonVar="World";
          Log.d("Test",singletonVar);
  
          Button btn=(Button)findViewById(R.id.button1);
          btn.setOnClickListener(btn2);
      }
  
  }
  </code></pre>
  
  <p>This is <code>NextActivity</code></p>
  
  <pre><code>public class NextActivity extends Activity {
  
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_next);
  
              String singletonVar = Singleton.customVar;
  
              Log.d("Test",singletonVar);
          }
    }
  </code></pre>
  
  <p><code>Singleton</code> Class</p>
  
  <pre><code>public class Singleton
  {
      private static Singleton instance;
  
      public static String customVar="Hello";
  
      public static void initInstance()
      {
      if (instance == null)
      {
        // Create the instance
        instance = new Singleton();
      }
      }
  
      public static Singleton getInstance()
      {
       // Return the instance
       return instance;
       }
  
       private Singleton()
       {
       // Constructor hidden because this is a singleton
       }
  
       public void customSingletonMethod()
       {
       // Custom method
       }
   }
  </code></pre>
  
  <p>and <code>MyApplication</code></p>
  
  <pre><code>public class MyApplication extends Application
      {
      @Override
      public void onCreate()
      {
      super.onCreate();
  
       // Initialize the singletons so their instances
       // are bound to the application process.
       initSingletons();
       }
  
       protected void initSingletons()
       {
       // Initialize the instance of MySingleton
       Singleton.initInstance();
       }
  
       public void customAppMethod()
       {
       // Custom application method
      }
  }
  </code></pre>
  
  <p>When i run this code, i get Hello which i have initialized in <code>Singleton</code> then World which i gave it in <code>MainActivity</code> and again shows Hello in <code>NextActivity</code> in logcat.
  I want it to show world again in <code>NextActivity</code>.
  Please help me to correct this.</p>