Singleton in Android -


i have followed link , made singleton class in android. http://www.devahead.com/blog/2011/06/extending-the-android-application-class-and-dealing-with-singleton/

problem want single object. have activity , activity b. in activity access object singleton class. use object , made changes it.

when move activity b , access object singleton class gave me initialized object , not keep changes have made in activity a. there other way save changing? please me experts. mainactivity

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 application instance     app = (myapplication)getapplication();      // call custom application method     app.customappmethod();      // call custom method in mysingleton     singleton.getinstance().customsingletonmethod();      singleton.getinstance();     // read value of 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); } 

}

this nextactivity

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);         }} 

singleton class

public class singleton { private static singleton instance;  public static string customvar="hello";  public static void initinstance() { if (instance == null) {   // create instance   instance = new singleton(); } }  public static singleton getinstance() {  // return instance  return instance;  }   private singleton()  {  // constructor hidden because singleton  }   public void customsingletonmethod()  {  // custom method  }  } 

and myapplication

public class myapplication extends application { @override public void oncreate() { super.oncreate();   // initialize singletons instances  // bound application process.  initsingletons();  }   protected void initsingletons()  {  // initialize instance of mysingleton  singleton.initinstance();  }   public void customappmethod()  {  // custom application method } } 

when run code, hello have initialized in singleton world gave in mainactivity , again shows hello in nextactivity in logcat. want show world again in nextactivity. please me correct this.

edit :

the implementation of singleton in android not safe , should use library dedicated kind of pattern dagger or other di library manage lifecycle , injection.


could post example code ?

take @ gist : https://gist.github.com/akayh/5566992

it works done :

myactivity : set singleton first time + initialize mstring attribute ("hello") in private constructor , show value ("hello")

set new value mstring : "singleton"

launch activityb , show mstring value. "singleton" appears...


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -