简体   繁体   English

最小化和还原应用程序后如何还原正确的视图状态?

[英]How restore correct state of view after minimize and restore application?

I try to use Mosby. 我尝试使用Mosby。 But I have one problem. 但是我有一个问题。

I have Activity (view), presenter, model, and viewState. 我有活动(视图),演示者,模型和viewState。 On activity I have button. 在活动中,我有按钮。 When I click on button I call the method from presenter to load data: 当我单击按钮时,我从演示者调用该方法以加载数据:

presenter.loadData()

Method that load data call 2 methods: 加载数据的方法调用2种方法:

1. view.showProgress 1. view.showProgress

  1. model.getData()

Method model.getData() create ASYNC http request (Retrofit). 方法model.getData()创建ASYNC http请求(Retrofit)。 After request finish than presenter call method from view, view.hideProgress() . 请求完成后,演示者将调用view.hideProgress()

OK. 好。 This scheme works fine. 这个方案很好用。

But I have one problem. 但是我有一个问题。 Steps: 脚步:

1.Click button on view 1.单击视图上的按钮

2. presenter.loadData() 2. presenter.loadData()

3.In presenter call view.showProgress() and start async http request 3.在主持人中调用view.showProgress()并启动异步 http请求

4.Minimize application 4.最小化应用

5.Activity is destroy 5,活动被摧毁

6.As result on viewState call method saveInstanceState() to save current state of view (progress is show) 6.作为viewState调用方法saveInstanceState()结果,以保存当前视图状态(显示进度)

7.After 5 seconds the network request was finished and presenter try to call view.hideProgress() 7. 5秒钟后,网络请求完成,演示者尝试调用view.hideProgress()

8.But Activity does not exist and as result, nothing happened. 8.但是活动不存在,结果什么也没发生。

9.User return to application 9.用户返回申请

  1. As result call method in viewState restoreInstanceState() 结果是viewState restoreInstanceState()调用方法

  2. And as result progress show again, because this state saved on viewState (see item 6). 结果再次显示进度,因为此状态保存在viewState上(请参见第6项)。

The problem is that the progress must be hidden because network request was successfully finished when application was minimized (not visible for the user). 问题是必须隐藏进度,因为在最小化应用程序时网络请求已成功完成(对于用户不可见)。

How can I fix problem? 我该如何解决问题?

When you receive HTTP response you should store it somewhere, for example, it can be repository which stores in memory, or maybe some local storage. 当您收到HTTP响应时,应将其存储在某个位置,例如,它可以是存储在内存中的存储repository ,也可以是一些local存储。 Then when you call model.getData() you should check if data is already saved, return this data from the model (and if needed send one more HTTP request to get fresher data) if there is no saved data - send HTTP request. 然后,当您调用model.getData() ,应检查是否已保存数据,如果没有保存的数据,则从模型返回此数据(如果需要,再发送一个HTTP请求以获取较新的数据)-发送HTTP请求。 So now, when activity opens you invoke presenter.getData() , it calls view.showProgress() inside and model.getData() , model returns some cached data (if available) and presented calls view.hideProgress() 因此,现在,当活动打开时,您调用presenter.getData() ,并在其中调用view.showProgress()model.getData() ,模型将返回一些缓存的数据(如果可用),并presented调用的view.hideProgress()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM