ViewState не сохраняется при повороте экрана в DialogFragment с Moxy - PullRequest
0 голосов
/ 06 ноября 2018

Я создал MvpBottomSheetDialogFragment на основе реализации MvpDialogFragment :

open class MvpBottomSheetDialogFragment : BottomSheetDialogFragment() {

    private var isStateSavedInternal = false

    private val mvpDelegate: MvpDelegate<out MvpBottomSheetDialogFragment> by lazy {
        MvpDelegate<MvpBottomSheetDialogFragment>(this)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        mvpDelegate.onCreate(savedInstanceState)
    }

    override fun onResume() {
        super.onResume()

        isStateSavedInternal = true

        mvpDelegate.onAttach()
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)

        isStateSavedInternal = true

        mvpDelegate.onSaveInstanceState()
        mvpDelegate.onDetach()
    }

    override fun onStop() {
        super.onStop()

        mvpDelegate.onDetach()
    }

    override fun onDestroyView() {
        super.onDestroyView()

        mvpDelegate.onDetach()
        mvpDelegate.onDestroyView()
    }

    override fun onDestroy() {
        super.onDestroy()

        // We leave the screen and respectively all fragments will be destroyed
        if (activity?.isFinishing == true) {
            mvpDelegate.onDestroy()
            return
        }

        // When we rotate device isRemoving() return true for fragment placed in backstack
        // http://stackoverflow.com/questions/34649126/fragment-back-stack-and-isremoving
        if (isStateSavedInternal) {
            isStateSavedInternal = false
            return
        }

        // See https://github.com/Arello-Mobile/Moxy/issues/24
        var anyParentIsRemoving = false

        if (Build.VERSION.SDK_INT >= 17) {
            var parent = parentFragment
            while (!anyParentIsRemoving && parent != null) {
                anyParentIsRemoving = parent.isRemoving
                parent = parent.parentFragment
            }
        }

        if (isRemoving || anyParentIsRemoving) {
            mvpDelegate.onDestroy()
        }
    }
}

Presenter:

@InjectViewState
class AuthPresenter : MvpPresenter<IAuthView>(), SubscribeablePresenter {

    override fun onFirstViewAttach() {
        super.onFirstViewAttach()

        Timber.e("First view attach")
    // ...
}

Я использую это так в своей деятельности:

        var authFragment = supportFragmentManager.findFragmentByTag(authFragmentTag)

        if (authFragment == null) {
            Timber.e("Creating dialog")
            authFragment = AuthBottomSheetFragment()

            authFragment.show(supportFragmentManager, authFragmentTag)
        }

И при повороте экрана я вижу в журналах, что диалог создается только один раз (запись журнала Creating dialog появляется только один раз), но onFirstViewAttach() вызывается каждый раз, когда я поворачиваю устройство, поэтому ViewState не сохраняется.

Почему?

...