Observer mode realizes communication between Activity and Fragment

Activity中定义被观察的数据:
 private class ClearShopCarObservable extends Observable {
        public void postMessage(Object eventtype) {
            setChanged();
            notifyObservers(eventtype);
        }
    }

private ClearShopCarObservable clearShopCarObservable= new ClearShopCarObservable();

private boolean edit = false;

public boolean isShopCarEdit() {
        return edit;
}

Where it is worth changing:


edit=true;

clearShopCarObservable.postMessage(edit);

Fragment 继承 Observer重写update:
 Boolean edit = ((Activity) activity).isShopCarEdit();

So you can get the value 

 

Guess you like

Origin blog.csdn.net/guodashen007/article/details/114259671