sockeqwe / mosby

A Model-View-Presenter / Model-View-Intent library for modern Android apps
http://hannesdorfmann.com/mosby/
Apache License 2.0
5.49k stars 841 forks source link

How restore correct state of view after minimize and restore application? #287

Closed alexei-28 closed 6 years ago

alexei-28 commented 6 years ago

I try to use Mosby. But I have one problem.

I have Activity (view), presenter ,model and viewState. On actvity I have button. When I click on button I call method from presenter to load data: presenter.loadData() Method that load data call 2 methods:

  1. `view.showProgress

2.model.getData()`

Method model.getData() create async http request (Retrofit). After request finish than presenter call method from view, view.hideProgress().

OK. Thise scheme work fine.

But I have one problem. Steps:

  1. Click button on view
  2. presenter.loadData()
  3. In presenter call view.showProgress() and start async http request
  4. Minimize application
  5. Activity is destroy
  6. As result on viewState call method saveInstanceState() to save current state of view (progress is show)
  7. After 5 seconds the network request was finsih and presenter try to call view.hideProgress()
  8. But Activity is not exist and as result nothing happend.
  9. User return to application
  10. As result call method in viewState restoreInstanceState()
  11. And as result progress show again, because this state was save on viewState (see item 6).

The problem is that the progress must be hide , because network request was success finish when application was mnimize (not visible for user).

How I can fix problem? `

sockeqwe commented 6 years ago

This issue is not Mosby related only. You will face the same issue if you use just Activities.

So there are two things to consider: If your network call lives outside of the lifecycle of Activity because the activity is really destroyed (which seems to be what you are trying to achieve) then you can't use / rely on activity.onSaveInstanceState() or Mosby's ViewState (which is essentially the same under the hood). You have to find another solution for that problem, like using Android Services / JobScheduler plus a Database where you save the state of your request persistently so that once Activity and Presenter gets recreated you can query the database if the request was successful or not.

If you are just talking about the fact that the view is just destroyed temporarily (so no view attached to Presenter) you may find MvpQueuingBasePresenter (take a look at onceViewAttached().