屏幕旋转后恢复可流动转换为实时数据 [英] Resume flowable converted to live data after screen rotation
问题描述
说我有这样的活动:
public class TestActivity extends AppCompatActivity {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TextView countdown = new TextView(this);
setContentView(countdown);
ViewModelProviders.of(this)
.get(TestViewModel.class)
.getCountdown()
.observe(this, countdown::setText);
}
}
视图模型是:
class TestViewModel extends ViewModel {
private final LiveData<String> countdown =
LiveDataReactiveStreams.fromPublisher(
Flowable.concat(
Flowable.just("Falcon Heavy rocket will launch in..."),
Flowable.intervalRange(0, 10, 3, 1, TimeUnit.SECONDS)
.map(x -> String.valueOf(10 - x)),
Flowable.timer(1, TimeUnit.SECONDS)
.map(ignored -> "Lift off!")
)
);
LiveData<String> getCountdown() {
return countdown;
}
}
我想正确处理旋转,所以如果当倒计时器显示5时,用户旋转他的设备,我希望旋转后的下一个值为5(如果第二个已经过去,则为4),只需在倒计时实际到达的任何地方进行调整。
I want to handle the rotation correctly, so if the user rotates his device while the countdown says 5, I want the next value after rotation to be 5 (or 4 if the second elapsed already), just pick up wherever the countdown should actually be.
如果火箭已经升空,我希望它在旋转后保持这种状态,我不希望倒计时再次开始。
If the rocket has already lifted off, I want it to keep it that way after rotation, I don't want the countdown to start again.
目前, LiveDataReactiveStreams
在暂停时取消订阅并在恢复时进行新订阅,因此倒计时重新启动。
At the moment, LiveDataReactiveStreams
cancels the subscription on pause and makes a new subscription on resume, so the countdown gets restarted.
我猜我应该在代码的RxJava部分更改一些内容以使其工作。关于改变什么的任何想法?
I'm guessing I should change something in my RxJava part of the code for this to work. Any ideas as to what to change?
推荐答案
感谢Svyatoslav Lebeckiy在 Android United Slack频道指出 BehaviorSubject
和 BehaviorProcessor
给我并提出了如何解决此问题的想法。
Thanks to Svyatoslav Lebeckiy over at Android United Slack channel who pointed out BehaviorSubject
and BehaviorProcessor
to me and proposed an idea on how to fix this.
我将我的视图模型更改为:
I changed my view model to:
class TestViewModel extends ViewModel {
private LiveData<String> countdown;
LiveData<String> getCountdown() {
if (countdown == null) {
countdown = LiveDataReactiveStreams.fromPublisher(startCountdown());
}
return countdown;
}
private static Flowable<String> startCountdown() {
final BehaviorProcessor<String> processor = BehaviorProcessor.create();
Flowable.concat(
Flowable.just("Falcon Heavy rocket will launch in..."),
Flowable.intervalRange(0, 10, 3, 1, TimeUnit.SECONDS)
.map(x -> String.valueOf(10 - x)),
Flowable.timer(1, TimeUnit.SECONDS)
.map(ignored -> "Lift off!")
).subscribe(processor);
return processor;
}
}
这样我只能在<在code> startCountdown LiveData )。
This way I can start the countdown only once in getCountdown
and the BehaviorProcessor
created in startCountdown
takes care of delivering the last and subsequently emitted values to its subscribers (LiveData
in this case).
由于 LiveDataReactiveStreams
需要一个 Flowable
,使用 BehaviorProcessor
代替<$ c $很方便c> BehaviorSubject 此处。
Since LiveDataReactiveStreams
needs a Flowable
, it's convenient to use a BehaviorProcessor
instead of BehaviorSubject
here.
这篇关于屏幕旋转后恢复可流动转换为实时数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!