开发者

Get a open Dialog without having any references to it

开发者 https://www.devze.com 2023-04-05 19:15 出处:网络
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?

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 onCreateDialogreturn 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消