LifecycleObservable for a click event in the background

I modified the RxAndroid example slightly to do the click task in the background, but it doesn't work :-(

Here's my code:

/**
 * Simple example of creating a Subscription that is bound to the lifecycle
 * (and thus automatically unsubscribed when the Activity is destroyed).
 */
public class LifecycleObservableActivity extends RxActivity {

    private static final String TAG = LifecycleObservableActivity.class.getSimpleName();

    private Button button;

    private Subscription subscription;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        button = new Button(this);
        button.setText("Click Me!");
        setContentView(button);
    }

    @Override
    protected void onStart() {

        super.onStart();

        Observable<OnClickEvent> clicks = ViewObservable.clicks(button)
                // .subscribeOn(Schedulers.computation());  IllegalStateException: Observers must subscribe from the main UI thread, but was Thread[RxComputationThreadPool-3,5,main]
                .observeOn(Schedulers.computation());

        subscription =
                LifecycleObservable.bindActivityLifecycle(lifecycle(), clicks)
                        .map(x -> {
                            Log.i(TAG, "Func1   - main thread: " + isCurrentlyOnMainThread() + " (" + Thread.currentThread() + ")");
                            return "hallo welt";
                        })
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(string -> {
                            Log.i(TAG, "Action1 - main thread: " + isCurrentlyOnMainThread() + " (" + Thread.currentThread() + ")");
                            Toast.makeText(LifecycleObservableActivity.this,
                                    string,
                                    Toast.LENGTH_SHORT)
                                    .show();
                        });
    }

    @Override
    protected void onPause() {

        super.onPause();

        // Should output "false"
        Log.i(TAG, "onPause(), isUnsubscribed=" + subscription.isUnsubscribed());
    }

    @Override
    protected void onStop() {

        super.onStop();

        // Should output "true"
        Log.i(TAG, "onStop(), isUnsubscribed=" + subscription.isUnsubscribed());
    }

    private boolean isCurrentlyOnMainThread() {

        return Looper.myLooper() == Looper.getMainLooper();
    }
}

      

When I run it I get this log:

Func1   - main thread: false (Thread[RxComputationThreadPool-3,5,main])
Action1 - main thread: true (Thread[main,5,main])
onPause(), isUnsubscribed=false
onStop(), isUnsubscribed=false

      

Func1 runs in the background, which is still right. But the subscription has not been unsubscribed. It only unsubscribes when Func1 is also running on the UI thread.

What do I need to change that Func1 is running in the background and the subscription will be unsubscribed?

+3


source to share


1 answer


Ok I think I got it:

    subscription = LifecycleObservable.bindActivityLifecycle(lifecycle(),
            AppObservable.bindActivity(this, ViewObservable.clicks(button))
                    .observeOn(Schedulers.computation())
                    .map(new Func1<OnClickEvent, String>() {
                        @Override
                        public String call(OnClickEvent onClickEvent) {
                            Log.i(TAG, "1 " + Thread.currentThread());
                            return ((Button) onClickEvent.view()).getText().toString();
                        }
                    })
                    .map(new Func1<String, String>() {
                        @Override
                        public String call(String o) {
                            Log.i(TAG, "2 " + Thread.currentThread());
                            return "hallo " + o;
                        }
                    })
                    .observeOn(AndroidSchedulers.mainThread()))
            .subscribe(new Action1<String>() {
                @Override
                public void call(String s) {
                    Log.i(TAG, "3 " + Thread.currentThread());
                    Log.i(TAG, "s: " + s);
                }
            });

      

I now bind the whole task to the Activity lifecycle, not just to ViewObservable.clicks(button)



With this change, I got this log:

1 Thread[RxComputationThreadPool-3,5,main]
2 Thread[RxComputationThreadPool-3,5,main]
3 Thread[main,5,main]
s: hallo ralph
onPause subscription isUnsubscribed: false
onStop subscription isUnsubscribed: true

      

+2


source







All Articles