Backback后,全局变量丢失的ApplicationContext

时间:2016-11-10 13:47:00

标签: java android android-context

我在名为Controller的全局类中声明的全局变量有一点问题,该类从应用程序扩展。

当我开始活动时AI在Controller中有一个带有0个项目的arraylist(这是一个购物车),我继续活动B,点击,我向“控制器”添加一个项目,我回去(带回来)按钮)在活动A和我得到一个arraylist仍然在0项有什么不对?

我的控制器类:

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

我的购物车类:

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;

    }
}

在onCreate()中获取控制器实例的BaseActivity:

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

    }

在我MainActivity extends BaseActivity (Activity A)中调用的onResume()中,我有一个刷新UI徽章的方法:

 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在OnClick()上扩展了BaseActivity我调用了一个简单的add:

mController.getCart().addItem(mItem);

2 个答案:

答案 0 :(得分:1)

制作ApplicationSingleton。像这样:

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

然后在使用Application课程时,请使用Controller.getInstance().getCart().addItem(mItem);

答案 1 :(得分:0)

我会考虑将静态模式用于此类操作,以便Cart对象在应用程序的整个生命周期中都是相同的。

以下是Controller的解决方案:

步骤1:在控制器类内部,将Cart设置为静态(将Cart的getter修改为静态)

private static Cart mCart;

步骤2:在Controller的onCreate中初始化实例

@Override
public void onCreate() {
    super.onCreate();
    mCart = new Cart();
}

使用方法:使用

从任何地方购买购物车
Controller.getCart();