RxAndroid: when to use bindActivity and why?

Is it really necessary to use AndroidObservable.bindActivity(...) in cases like activity below? Since you still need to unsubscribe manually in onDestroy .

According to this blog , bindActivity is needed like this:

you do not accidentally try to change state after this is true.

which, as I read it, will mean that there may be problems when you perform certain actions with activity after calling finish() , and therefore the cancellation in onDestroy will be too late.

Can someone give me an example of when unsubscribing already in onDestroy would be a problem?


If you look at the source for AndroidObservable.java , the predicate function used for bindActivity:

 private static final Func1<Activity, Boolean> ACTIVITY_VALIDATOR = new Func1<Activity, Boolean>() { @Override public Boolean call(Activity activity) { return !activity.isFinishing(); } }; 

It would not be better to check configuration changes, for example:

 private static final Func1<Activity, Boolean> ACTIVITY_VALIDATOR = new Func1<Activity, Boolean>() { @Override public Boolean call(Activity activity) { return !activity.isFinishing() && !activity.isChangingConfigurations(); } }; 

???


SomeActivity.java

 public class SomeActivity extends Activity implements Observer<String> { private Subscription subscription; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); subscription = getObservable().observeOn(AndroidSchedulers.mainThread()).subscribe(this); // or use bindActivity here???? // subscription = AndroidObservable.bindActivity(this, getObservable()).subscribe(this); } @Override protected void onDestroy() { super.onDestroy(); subscription.unsubscribe(); } @Override public void onNext(String s) { // do something with the activity state... } @Override public void onCompleted() {} @Override public void onError(Throwable throwable) {} } 
+5
source share
1 answer

You cannot perform fragment operations after onSaveInstanceState been called, so if your subscription opens DialogFragment and then does it before onDestroy , but after onSaveInstanceState your application will crash. This will probably happen if the action closes and the network request that wants to display an error ends. Anything that requires a save state and therefore cannot be called after onSaveInstanceState will be a problem.

+4
source

Source: https://habr.com/ru/post/1204581/


All Articles