Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is onResume method of a Fragment never fired after dismissing a DialogFragment which was launched from the Fragment?

I am going to explain my problem as short as possible.

I have a Fragment called FragmentA which displays a DialogFragment after clicking on a specific button.

public class FragmentA extends Fragment implements OnClickListener {

    ...

    @Override
    public void OnClick(View v) {
        if (v == dialogButton) {
            showDialog();
        }
    }

    public void showDialog() {
        String diagName = getResources().getString(R.string.dialog_title);
        MyDialog myDialog = MyDialog.newInstance(getFragmentAValue());
        myDialog.show(getFragmentManager(), diagName);
    }
}

public class MyDialog extends DialogFragment implements OnClickListener {
    ...
    @Override
    public void onClick(View view) {
        if (view == acceptButton) {
            ...
        }
        else if (view == cancelButton) {
            ...
        }
    }
}

The dialog is dispalyed without any problem. But my problem consists in after myDialog is dismissed onResume() method in FragmentA is never called and FragmentA is shown and you can interact with it without any problem.

public class FragmentA extends Fragment implements OnClickListener {

    ...

    @Override
    public void onResume() {
        super.onResume();
        resumeFragmentA();
    }
}

So, what I have done in order to fix this issue is copying an instance of FragmentA in the end of onActivityCreated() method and call the method resumeFragmentA() when user has closed the dialog.

public class FragmentA extends Fragment implements OnClickListener {

    FragmentA fragmentA = null;

    ...

    @Override
    public void onResume() {
        super.onResume();
        resumeFragmentA();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {

        ...

        fragmentA = this;
    }

    ...
}

public class MyDialog extends DialogFragment implements OnClickListener {
    ...
    @Override
    public void onClick(View view) {
        if (view == acceptButton) {
            storeData();
            dismissDialog();
        }
        else if (view == cancelButton) {
            dismissDialog();
        }
    }

    public void dismissDialog() {
        FragmentA.fragmentA.resumeFragmentA();
        dismiss();
    }

}

I know this solution is tricky but I do not know to solve in a more brilliant way. Do you know it? Any Idea? Thanks in advance!

For a better reading of my code, here you have my full code:

public class FragmentA extends Fragment implements OnClickListener {

    ...

    FragmentA fragmentA = null;

    ...

    @Override
    public void OnClick(View v) {
        if (v == dialogButton) {
            showDialog();
        }
    }

    public void showDialog() {
        String diagName = getResources().getString(R.string.dialog_title);
        MyDialog myDialog = MyDialog.newInstance(getFragmentAValue());
        myDialog.show(getFragmentManager(), diagName);
    }

    @Override
    public void onResume() {
        super.onResume();
        resumeFragmentA();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {

        ...

        fragmentA = this;
    }

    ...
}


public class MyDialog extends DialogFragment implements OnClickListener {

    ...

    static MyDialog newInstance(int value) {
        MyDialog fragment = new MyDialog ();
        Bundle args = new Bundle();
        args.putInt("value", value);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onClick(View view) {
        if (view == acceptButton) {
            storeData();
            dismissDialog();
        }
        else if (view == cancelButton) {
            dismissDialog();
        }
    }

    public void dismissDialog() {
        FragmentA.fragmentA.resumeFragmentA();
        dismiss();
    }

    ...

}
like image 747
José Carlos Avatar asked Oct 05 '13 11:10

José Carlos


People also ask

What is the difference between dialog & DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.

How do you dismiss a DialogFragment?

tl;dr: The correct way to close a DialogFragment is to use dismiss() directly on the DialogFragment. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog.

Is DialogFragment deprecated?

This class was deprecated in API level 28.

What is DialogFragment used for?

DialogFragment is a utility class which extends the Fragment class. It is a part of the v4 support library and is used to display an overlay modal window within an activity that floats on top of the rest of the content. Essentially a DialogFragment displays a Dialog but inside a Fragment.


1 Answers

If you just want to call onResume of FragmentA, make a call to startActivityForResult inside your DialogFragment, and start your FragmentActivity. This will call onResume.

public void dismissDialog() {
   getActivity().startActivityForResult(getActivity().getIntent(), 10);
   dismiss()
}
like image 154
MRK Avatar answered Sep 20 '22 17:09

MRK