Calling Fragment not paused when showing DialogFragment
Asked Answered
C

3

11

In one part of my application, I show the user a ListView. When the user presses an item in the list, a DialogFragment is shown.

@Override
public void onClick() {
    android.support.v4.app.FragmentTransaction ft = getFragment().getFragmentManager().beginTransaction();
    ft.addToBackStack(null);
    SingleSettingDialogFragment dialog = SingleSettingDialogFragment.newInstance(...);
    dialog.show(ft, "Single");
}

The DialogFragment have the following structure:

@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
    super.onCreateDialog(savedInstanceSate);
    AlertDialog dialog =  new AlertDialog.Builder(...)
        ...
        .create();
    ...
    return dialog;
}

When the user exits the DialogFragment, I expect the onResume() method of the calling fragment to be called, but it is not.

So what's so special about DialogFragments? Why aren't the calling Fragment paused when the Dialog is shown? And how can it be achieved?

I haven't found any reference to this behaviour in the Docs, so references is a plus.

Connate answered 17/9, 2015 at 10:31 Comment(0)
B
6

This may help you: link

In fact a FragmentDialog is not an activity on itself but it is part of the same activity which contains the calling fragment. It means that the fragment is not paused when dialogFragment is shown.

Citing the source I gave you, a fragment is paused when: Another activity is in the foreground and has focus, but the activity in which this fragment lives is still visible (the foreground activity is partially transparent or doesn't cover the entire screen).

Hope that helps

Bosket answered 17/9, 2015 at 11:33 Comment(2)
This is not really what I asked. I know that the DialogFragment is not an activity itself and I do not expect my activity to recieve on pause events. I expect that my fragment that is requesting a DialogFragment is paused when the DialogFragment is shown. Just as if any other fragment would be shown.Connate
What the text means is that the fragment is Paused only when the activity is paused. Two fragment, hosted by the same activity, such as in your case, can exist one over the other beeing both active. That is why your fragment is not paused.Bosket
K
1

One way to deal with this is to embed your DialogFragment within an Activity and display the activity as a Dialog, there's a tip in the following link that explains how:

http://developer.android.com/guide/topics/ui/dialogs.html

You can use this to update the underlying Fragment because when the Dialog (which is an Activity) is finished, onResume() will be called on the underlying fragment. Add code to update the state of the fragment in the onResume() method and that's all there is too it.

Other approach is that you can override OnDismiss() method and use call back listener in it which will call back the parent fragment.

These are the suggestions from my side, Hope it will get any kind of clue.

Kerrikerrie answered 17/9, 2015 at 14:6 Comment(0)
B
1

When you open a DialogFragment or BottomSheetDialogFragment, the onPause and onResume methods of the calling fragment are not called.

To handle this, you can use FragmentLifecycleCallbacks:

1- Create a FragmentLifecycleCallbacks (inner) class:

// Create a FragmentLifecycleCallbacks (inner) class:
    inner class ChildFragmentCallbacks : FragmentManager.FragmentLifecycleCallbacks() {
        override fun onFragmentResumed(fm: FragmentManager, f: Fragment) {
            super.onFragmentResumed(fm, f)
            // dialogFragments/bottomsheetfragments oppend
        }

        override fun onFragmentPaused(fm: FragmentManager, f: Fragment) {
            super.onFragmentPaused(fm, f)
            // dialogFragments/bottomsheetfragments closed
        }
    }

2- Register it on onCreate():

childFragmentManager.registerFragmentLifecycleCallbacks(ChildFragmentCallbacks(), false)

total code:

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager

class YourParentFragment : Fragment() {

    // ...

    // Create a FragmentLifecycleCallbacks (inner) class:
    inner class ChildFragmentCallbacks : FragmentManager.FragmentLifecycleCallbacks() {
        override fun onFragmentResumed(fm: FragmentManager, f: Fragment) {
            super.onFragmentResumed(fm, f)
            // dialogFragments/bottomsheetfragments oppend
        }

        override fun onFragmentPaused(fm: FragmentManager, f: Fragment) {
            super.onFragmentPaused(fm, f)
            // dialogFragments/bottomsheetfragments closed
        }
    }

    // Register it on onCreate():
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        childFragmentManager.registerFragmentLifecycleCallbacks(ChildFragmentCallbacks(), false)
    }

    // ...

}

Bases answered 1/8, 2023 at 10:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.