java - ApplicationContext with global variable lost after back pressed -


i have little problem global variable declared in global class called controller extend application.

when start activity have arraylist 0 items in controller( it's cart), go on activity b, on click, add item "controller", go (with button) on activity , arraylist still @ 0 items what's wrong ?

my controller class :

public class controller extends application {      public static final int signup_request = 98;  // request code     public static final int signin_request = 99;     private user muser;// instance de l'utilisateur connecté     private cart mcart = new cart(); // panier     private tracker mtracker; // analytics     private session msession;     private sessionmodule msessionmodule;      @override     public void oncreate() {         super.oncreate();      }      @override     protected void attachbasecontext(context base) {         super.attachbasecontext(base);         multidex.install(this);         msessionmodule = new sessionmodule(base);         msession = msessionmodule.getsession();     }      public cart getcart() {          return mcart;     }      public session getsession() {         return msession;     }      public user getuser() {         return muser;     }      public void setuser(user muser) {         this.muser = muser;     } } 

my cart class :

public class cart {     private arraylist<item> items i= new arraylist<item>();     private store mstore;      public item getitem(int pposition) {          return items.get(pposition);     }      public void additem(item item) {         items.add(item);     }      public void removeitem(int pposition) {         items.remove(pposition);     }      public void clear() {         items.clear();     }      public int size() {         return items.size();     }      public arraylist<item> getitems() {          return items;      } } 

a baseactivity instance of controller in oncreate() :

public abstract class baseactivity extends appcompatactivity {     public session msession;     public controller mcontroller;      public static final int signup_request = 98;  // request code     public static final int signin_request = 99;      public static final int login_request = 1;  // request code     public static final int register_request = 2;  // request code      @override protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         mcontroller=(controller)getapplicationcontext();          msession=mcontroller.getsession();      } 

on mainactivity extends baseactivity (activity a) called in onresume(), have method refresh ui badges :

 public void checkcartitems(){         if(itemcart!=null){             layerdrawable iconcart = (layerdrawable) itemcart.geticon();             int nb_items=mcontroller.getcart().size();             tools.setbadgecount(this, iconcart,string.valueof(nb_items));         }      } 

on activity b extends baseactivity on onclick() call simple add :

mcontroller.getcart().additem(mitem); 

make application class singleton. this:

public class controller extends application {      public static final int signup_request = 98;  // request code     public static final int signin_request = 99;     private user muser;// instance de l'utilisateur connecté     private cart mcart = new cart(); // panier     private tracker mtracker; // analytics     private session msession;     private sessionmodule msessionmodule;     private static controller _minstance;      @override     public void oncreate() {         super.oncreate();         _minstance = this;      }      @override     protected void attachbasecontext(context base) {         super.attachbasecontext(base);         multidex.install(this);         msessionmodule = new sessionmodule(base);         msession = msessionmodule.getsession();     }      public static synchronized controller getinstance() {         return _minstance;     }      public cart getcart() {          return mcart;     }      public session getsession() {         return msession;     }      public user getuser() {         return muser;     }      public void setuser(user muser) {         this.muser = muser;     } } 

then while using application class, use controller.getinstance().getcart().additem(mitem);


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -