gpt4 book ai didi

java - 后台点击事件的生命周期可观察

转载 作者:太空狗 更新时间:2023-10-29 13:19:59 28 4
gpt4 key购买 nike

我更改了 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 目前在后台运行。但订阅并没有退订。只有当 Func1 也在 UI 线程中运行时,它才会取消订阅。

我必须更改 Func1 在后台运行并且订阅将取消订阅吗?

最佳答案

好的,我想我明白了:

    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);
}
});

现在我将整个任务绑定(bind)到 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

关于java - 后台点击事件的生命周期可观察,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30156619/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com