What is lifecycle of DialogFragment
Asked Answered
D

3

29

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?

enter image description here

Dismember answered 31/7, 2018 at 13:42 Comment(4)
Here you go official documentation .developer.android.com/reference/android/app/…Curbstone
this may help you developer.android.com/reference/android/app/…Alloplasm
@NileshRathod & Umair, I checked this also before posting question. Please let me know, do you get a satisfying answer from this documentation?Dismember
@Khemraj dialogFragment's life cycle is similar to fragment.Curbstone
C
61

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();
}
Curbstone answered 31/7, 2018 at 14:1 Comment(4)
Thanks, I could also check lifecycle by creating logs, but I posted question. Because SO should be a good collection of questions. You should also add onStop, onDestroy etc. also to make answer good.Dismember
@Khemraj yes it should be, dialogFragment just act like a fragment because if you look closely in documentation dialogFragment extends Fragment which implements callbacks. Happy Coding :)Curbstone
It is understood, I mentioned in question, that DialogFragment extends Fragment. But I did not know the sequence on onCreateDialog.Dismember
That's not true. onCreateDialog is never guaranteed to be called before onStart/onResume. Basically it's not part of the "true" lifecycle.Strand
F
6

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.

Fitzsimmons answered 3/4, 2019 at 10:18 Comment(0)
Q
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

Quade answered 31/7, 2018 at 13:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.