如何在活动之间传递ObservableInt?

时间:2017-06-14 06:19:16

标签: java android android-activity observable android-databinding

我在Activity1中有ObservableInt,并希望通过意图将其传递给Activity2。但无论我如何通过它,OnPropertyChangedCallback绑定到ObservableInt都会丢失。如何防止丢失?

// Activity1
private ObservableInt var = new ObservableInt(0);
var.addOnPropertyChangedCallback(callback1);
public void goToActivity2(View view) {
    var.set(1); // fires callback1
    Intent intent = new Intent(getContext(), Activity2.class);
    // first way to pass ObservableInt
    intent.putExtra("VAR", (Parcelable) var);
    // second way to pass ObservableInt
    Bundle bundle = new Bundle();
    bundle.putParcelable("VAR", var);
    intent.putExtras(bundle);
    startActivity(intent);
}
// Activity2
protected void onCreate(Bundle savedInstanceState) {
    ObservableInt a = getIntent().getParcelableExtra("VAR");
    Bundle bundle = getIntent().getExtras();
    ObservableInt b = bundle.getParcelable("VAR");
    a.set(2); // cannot fire callback1
    b.set(3); // cannot fire callback1
}
//Activity1
private ObservableInt var = new Observable(0);
protected void onCreate(Bundle savedInstanceState) {
    var.addOnPropertyChangedCallback(callback1);
    var.set(1); // fires callback1
    Fragment1 frag = Fragment1.newInstance(var); // instantiates Fragment1
    // inflate some layout with frag
}

public void goToActivity2 (View view) {
    Intent intent = new Intent(this, goToActivity2.class);
    intent.putExtra("VAR", var);
    startActivity(intent);
}

// Fragment1
public static Fragment1 newInstance(ObservableInt var) {
    Bundle args = new Bundle();
    args.putSerializable("VAR", var);
    Fragment1 fragment = new Fragment1();
    fragment.setArguments(args);
    return fragment;
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    ObservableInt var = (ObservableInt) getArguments().getSerializable("VAR");
    var.addOnPropertyChangedCallback(callback2);
    var.set(2); // fires callback1 and callback2
}

//Activity2
protected void onCreate(Bundle savedInstanceState) {
    ObservableInt var= (ObservableInt) getIntent().getSerializableExtra("VAR");
    var.addOnPropertyChangedCallback(callback3);
    var.set(3); // only fires callback3 :(
}

2 个答案:

答案 0 :(得分:1)

当你传递Parcelable时,从另一边出来的对象不是同一个实例。

the docs

  

此类是可分区和可序列化的,但在对象被分区/序列化时会忽略回调。

答案 1 :(得分:0)

不要通过Intent传递Observable,而是使用带RxJava的事件总线样式:

public class RxEventBus {

    private static RxEventBus instance;

    private PublishSubject<Observable> observable_bus = PublishSubject.create();

    public static RxEventBus getInstance() {
        if (instance == null) {
            instance = new RxEventBus();
        }
        return instance;
    }

    public void sendBusObservable(Observable observable_bus) {
        observable_bus.onNext(observable_bus);
    }

    public Observable<Observable> getBusObject() {
        return observable_bus;
    }
  }

Activity1 传递Observable,

private ObservableInt var = new ObservableInt(0);
var.addOnPropertyChangedCallback(callback1);

RxEventBus.getInstance().sendBusObservable(var);

活动2 订阅数据更改,

 RxEventBus rxEventBus = RxEventBus.getInstance();
        rxEventBus.getBusObject()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(a -> {
                    if (a instanceof Observable) {
                        Observable observable = (Observable) a;
                        // do the rest with received observable
                    }
                });