Reputation: 3294
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.
Upvotes: 11
Views: 7576
Reputation: 226
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)
}
// ...
}
Upvotes: 1
Reputation: 523
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.
Upvotes: 1
Reputation: 4577
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
Upvotes: 6