Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does DialogFragment affect life cycle of calling activity

If I launch a DialogFragment from an activity, what happens when I close the DialogFragment? Does the activity go through onResume state? Or is the call as any normal java call so that the next line is never executed until the DialogFragment is closed?

Suppose the method for launching my fragment is

private void launchFragment(){
    ConfirmationDialog confirm = new ConfirmationDialog();
    confirm.show(getSupportFragmentManager(), "confirm");
    doMoreStuff();
}

So my question is twofold:

  1. When is doMoreStuff() called? Before or after I close the fragment to return to the parent activity?
  2. After I close the fragment to return to the parent activity, does the parent activity go through onResume: so that if I have a check for some field changed by the fragment, I can do work in onResume based on the state of that field:

As in the following example:

@Override
public void onResume() {
   super.onResume();
   if(dialogFragmentChangedThis){
      workSomeMore();
   }
}

The fragment is launched with

setStyle(STYLE_NO_FRAME, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);

so that it is in fullscreen mode.

like image 228
Katedral Pillon Avatar asked Apr 08 '14 17:04

Katedral Pillon


1 Answers

When is doMoreStuff() called? Before or after I close the fragment to return to the parent activity?

DialogFragment's show() method just commits a FragmentTransaction, which will be executed asynchronously, so any method that follows it will be executed immediately.

After I close the fragment to return to the parent activity, does the parent activity go through onResume

No, it doesn't, since your Activity is in foreground all the time, as there are no other Activities involved.

Usually you'll use the Callbacks listeners with DialogFragments as you do with simple Fragments, by implementing the Callbacks in your Activity and by calling your Activity's method through that interface when you want to pass a result of an action that happened in the DialogFragment.

like image 155
Egor Avatar answered Oct 23 '22 19:10

Egor