后台点击事件的生命周期可观察
LifecycleObservable for click event in background
我稍微更改了 RxAndroid example 以在后台执行点击任务,但它不起作用:-(
这是我的代码:
/**
* 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();
}
}
当我 运行 它时,我得到了这个日志:
Func1 - main thread: false (Thread[RxComputationThreadPool-3,5,main])
Action1 - main thread: true (Thread[main,5,main])
onPause(), isUnsubscribed=false
onStop(), isUnsubscribed=false
到目前为止,Func1 运行s 在后台是正确的。但订阅并没有退订。它仅在 Func1 也在 UI 线程中 运行s 时取消订阅。
我必须在后台更改 Func1 运行s 并且订阅将取消订阅?
好的,我想我明白了:
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);
}
});
现在我将整个任务绑定到 Activity 生命周期,而不仅仅是 ViewObservable.clicks(button)
有了这个改变,我得到了这个日志:
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
我稍微更改了 RxAndroid example 以在后台执行点击任务,但它不起作用:-(
这是我的代码:
/**
* 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();
}
}
当我 运行 它时,我得到了这个日志:
Func1 - main thread: false (Thread[RxComputationThreadPool-3,5,main])
Action1 - main thread: true (Thread[main,5,main])
onPause(), isUnsubscribed=false
onStop(), isUnsubscribed=false
到目前为止,Func1 运行s 在后台是正确的。但订阅并没有退订。它仅在 Func1 也在 UI 线程中 运行s 时取消订阅。
我必须在后台更改 Func1 运行s 并且订阅将取消订阅?
好的,我想我明白了:
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);
}
});
现在我将整个任务绑定到 Activity 生命周期,而不仅仅是 ViewObservable.clicks(button)
有了这个改变,我得到了这个日志:
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