What is lifecycle of DialogFragment

扶醉桌前 提交于 2019-11-29 13:47:38

问题


I could not find proper lifecycle of android.support.v4.app.DialogFragment by searching on Google. I need this for some implementation. As we know DialogFragment has some methods same like Dialog.

DialogFragment extends Fragment so its lifecycle is same as Fragment. But what about other methods of DialogFragment?

Here is Fragment lifecycle. Can one provide for DialogFragment?


回答1:


DialogFragment life cycle is similar to the life cycle of fragment:. To test yourself put logs in each of the overrided methods of dialogFragment and then run your code you will understand the working of dialogFragment.

onAttach
onCreate
onCreateDialog
onCreateView
onActivityCreated
onStart
onResume

And as far as finishing or destroying dialogFragment is concerned the lifeCycle is as follows:

onPause
onStop
onDestroyView
onDestroy
onDetach

Also I believe this method will also help you know the lifecycle :

@NonNull
@Override
public Lifecycle getLifecycle() {
    return super.getLifecycle();
}



回答2:


DialogFragment does various things to keep the fragment's lifecycle driving it, instead of the Dialog. Note that dialogs are generally autonomous entities -- they are their own window, receiving their own input events, and often deciding on their own when to disappear (by receiving a back key event or the user clicking on a button).

Source : https://developer.android.com/reference/android/app/DialogFragment#lifecycle




回答3:


Strange, that if you created an AlertDialog in onCreateDialog(), didn't call onCreateView(), then onViewCreated() wouldn't also call.

See Android DialogFragment onViewCreated not called and OnCreateView not called in a dialogfragment from a fragment.



来源:https://stackoverflow.com/questions/51614834/what-is-lifecycle-of-dialogfragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!