简体   繁体   中英

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 (eg, 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM