GCX-HCI / ThirtyInch

a MVP library for Android favoring a stateful Presenter
Apache License 2.0
1.03k stars 101 forks source link

Why saving the Presenter on orientation change #157

Closed mikaelzero closed 6 years ago

mikaelzero commented 6 years ago

Hello, I have seen your source code, there is a question, why save the Presenter when the screen is rotated or the configuration changes? What happens if I don't save?

StefMa commented 6 years ago

@miaoyongjun that is the "core"/"main" feature of ThirtyInch. We save the Presenter on orientation change to save the current state of your View.

Imagine the following scenario: A user wants to sign in into your app. He is enter is username and password, press then "sign in". Pressing the button leads to a `presenter.onSignInClicked("username", "password") and in this method we have something like this

fun onSignInClicked(username: String, password: String) {
   singInUseCase.execute(username, password)
        .subscribe{} // User is signed in now
}

Imagine that the user rotates the device while we call execute. Means the execution is "running". When we save the Presenter we can call (inside the subcribe{}) something like

deliverToView {
  showSuccessfullySignInToast()
  closeView()
}

And everything works perfectly.

Imagine if we destroy the Presenter on orientation change (we don't save the Presenter!). Then - after the orientation change - the user is maybe signed in (because we don't know if the "old" subscribe was successfully or not) and we have to restore the view state by some other logic.

But - as you say. You are totally free to say if you want that behavior or not.