22

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

Khemraj Sharma
  • 46,529
  • 18
  • 168
  • 182
  • Here you go official documentation .https://developer.android.com/reference/android/app/DialogFragment#Lifecycle – Umair Jul 31 '18 at 13:45
  • this may help you https://developer.android.com/reference/android/app/DialogFragment#Lifecycle – AskNilesh Jul 31 '18 at 13:45
  • @NileshRathod & Umair, I checked this also before posting question. Please let me know, do you get a satisfying answer from this documentation? – Khemraj Sharma Jul 31 '18 at 13:49
  • 1
    @Khemraj dialogFragment's life cycle is similar to fragment. – Umair Jul 31 '18 at 14:02

3 Answers3

42

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();
}
Umair
  • 5,756
  • 15
  • 39
  • 47
  • 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. – Khemraj Sharma Jul 31 '18 at 14:07
  • @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 :) – Umair Jul 31 '18 at 14:10
  • It is understood, I mentioned in question, that DialogFragment extends Fragment. But I did not know the sequence on onCreateDialog. – Khemraj Sharma Jul 31 '18 at 14:11
  • 2
    That's not true. `onCreateDialog` is never guaranteed to be called before `onStart`/`onResume`. Basically it's not part of the "true" lifecycle. – Farid Sep 03 '20 at 08:31
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

2

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.

CoolMind
  • 16,738
  • 10
  • 131
  • 165