WhatsUp WhatsUp - 24 days ago 11
Android Question

ApplicationContext with global variable lost after back pressed

I have a little problem with my global variable declared in a global class called

Controller
which extend from application.

When I start Activity A I have an arraylist with 0 items in
Controller
( it's a cart), I go on activity B, on click, I add an item to "Controller", I go back (with back button) on activity A and I get an arraylist still at 0 items what's wrong ?

My controller class :

public class Controller extends Application {

public static final int SIGNUP_REQUEST = 98; // The 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 which get the instance of controller in onCreate() :

public abstract class BaseActivity extends AppCompatActivity {
public Session mSession;
public Controller mController;

public static final int SIGNUP_REQUEST = 98; // The request code
public static final int SIGNIN_REQUEST = 99;

public static final int LOGIN_REQUEST = 1; // The request code
public static final int REGISTER_REQUEST = 2; // The request code

@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mController=(Controller)getApplicationContext();

mSession=mController.getSession();

}


On my
MainActivity extends BaseActivity (Activity A)
called in
onResume()
, I have an method which 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() I call a simple add :

mController.getCart().addItem(mItem);

Answer

Make your Application class Singleton. Like this:

public class Controller extends Application {

    public static final int SIGNUP_REQUEST = 98;  // The 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