Get a open Dialog without having any references to it
On Android, if I 开发者_JS百科added a Dialog into the screen and another Activity opens, how can I get from that Activity all the open Dialogs?
I need to close one of them.
You should create a class and extend DialogFragment
and in onCreateDialog
return your dialog, then show this dialog fragment with a specific tag , after that , you can find this dialog by using activity.getFragmentManager().findFragmentByTag("the tag")
.
Don't hold a reference to your dialog (local or global) because the activity can get recreated and it will leak the reference in the memory, just get the reference with the findFragment method.
If you are in a fragment , you can use fragment.getChildFragmentManager(...)
and do the same.
Good luck!
When another activity opens, your current activity will go through the onPause() state. More info on it can be found here.
In the onPause() call dialog.dismiss() and you should be set.
精彩评论