Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get reason for Android DialogFragment onDismiss() being called?

In my DialogFragment, I override onDismiss() in order to provide a callback to the parent fragment to notify it when the dialog has been dismissed by the user.

However, onDismiss() is also called for config change (e.g., when the device is rotated) - and I do not want a callback in this case.

So I have been trying to find a way to distinguish between onDismiss() being called for a user interaction and for a config change, but haven't had any luck.

For each of the two scenarios, these checks return the same values in my onDismiss() method:

isRemoving() == false
isDetached() == false
isAdded() == true
isInLayout() == false
isVisible() == false

I've also tried overriding onConfigurationChanged(Configuration newConfig), but that isn't called at all.

Any ideas how to get the cause of onDismiss() being called?

like image 795
ban-geoengineering Avatar asked Sep 01 '25 05:09

ban-geoengineering


1 Answers

If the users dismiss the dialog by pressing BACK or touching the area outside of the dialog window, onCancel() will be called before onDismiss().

If the users dismiss the dialog by clicking a Button, you will be able to catch the click event.

So you can solve the problem by overriding onCancel() instead of onDismiss() and keeping track of the click events.

like image 81
Bö macht Blau Avatar answered Sep 02 '25 19:09

Bö macht Blau