mosby icon indicating copy to clipboard operation
mosby copied to clipboard

MVP ViewState, restoreInstanceState is not called

Open kirilamenski opened this issue 5 years ago • 0 comments

Hi, I faced with some issue and would be very appriciate if you could help with it. I use gradle com.hannesdorfmann.mosby3:viewstate:3.1.1

This is my Activity class:

class MainActivity : BaseViewStateActivity<MainActivityView, MainActivityPresenter, MainViewState>(), MainActivityView {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    override fun onPostCreate(savedInstanceState: Bundle?) {
        super.onPostCreate(savedInstanceState)
    }

    override fun onNewViewStateInstance() {
        presenter.nextPage()
    }

    override fun createPresenter(): MainActivityPresenter = MainActivityPresenter()

    override fun createViewState(): MainViewState = MainViewState()

and ViewState:

class MainViewState : RestorableViewState<MainActivityView> {

    private val keyState = "MainViewState.KEY"

    var data = ArrayList<*>()

    override fun saveInstanceState(out: Bundle) {
        out.putString(keyState, "123")
    }

    override fun restoreInstanceState(args: Bundle?): RestorableViewState<MainActivityView> {
        var savedString = args?.getString(keyState)
        return this
    }

    override fun apply(view: MainActivityView?, retained: Boolean) {
        safeLet(view, data) { v, data ->
            v.updateRecycleView(data)
        }
    }

}

The problem is that restoreInstanceState is never called. Could you please explain me the logic in
https://github.com/sockeqwe/mosby/blob/7f22118c950b5e6fbba0ec42d8e97586dfb08940/viewstate/src/main/java/com/hannesdorfmann/mosby3/mvp/delegate/ActivityMvpViewStateDelegateImpl.java#L75

The first time an Activity is started, restoreInstanceState is not called, because the bundle is null

if (bundle != null && viewState instanceof RestorableViewState) {
    ...
      RestorableViewState restoredViewState =
          ((RestorableViewState) viewState).restoreInstanceState(bundle);
    ...
}

but after rotate the screen, the method does not call as the program stops at line 92 (where return is called)

if (mosbyViewId != null) {
      VS viewState = PresenterManager.getViewState(activity, mosbyViewId);
      if (viewState != null) {
        //
        // ViewState restored from PresenterManager
        //
        setViewState(viewState, true, true);
        if (DEBUG) {
          Log.d(DEBUG_TAG, "ViewState reused from Mosby internal cache for view: "
              + delegateCallback.getMvpView()
              + " viewState: "
              + viewState);
        }

        return;
      }
    }

why is "return" called?

thank you in advance, Regards!

kirilamenski avatar May 27 '19 08:05 kirilamenski