我已经基于MvpDialogFragment implementation创建了MvpBottomSheetDialogFragment:
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()
}
}
}
演示者:
@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不是存储。
为什么?