Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MVP ViewState, restoreInstanceState is not called #333

Open
kirilamenski opened this issue May 27, 2019 · 0 comments
Open

MVP ViewState, restoreInstanceState is not called #333

kirilamenski opened this issue May 27, 2019 · 0 comments

Comments

@kirilamenski
Copy link

kirilamenski commented May 27, 2019

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

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant