Displaying a AlertDialog on screen irrespective of Activity Screen

守給你的承諾、 提交于 2019-12-12 04:25:13

问题


I want to show an Dialog Whatever the screen the User is in. Suppose if user opens application and in initial screen if I receive a server message I have to show it in a dialog.Meanwhile there is an option of autologin . So it could move to my next activity.If this is the case that dialog should not be closed.It should show on newly opened activity rather than the previous activity.And other thing is that even though the dialog is shown I should be able to control my buttons on the activity.

Here's what i am doing.

  if(Activity1.mcontext!=null){
                                    CommonMethods.showDialog(sliderMessageText, 

                                    LoginActivity.mcontext,"activity1");
                                }
                                if(Activity2.context!=null){
                                    CommonMethods.showSliderMessageText(sliderMessageText, 

                                            Activity2.context,"activity2");
                                }
                                if(Activity3.mcontext!=null){
                                    CommonMethods.showSliderMessageText(sliderMessageText,Activity3.context,"activity3");
}

Instead I am displaying the dialog in all the activities.

Thanks in advance.


回答1:


You may want to consider using an Event driven model such as GreenRobot.

http://greenrobot.org/eventbus/

This would allow you to efficiently handle the scenario you describe.

Alternatively, you can use the LocalBroadcastManager to communicate between different parts of your app.

e.g Send a broadcast message when you want to display a dialog and handle that message in all of your Activities



来源:https://stackoverflow.com/questions/42003140/displaying-a-alertdialog-on-screen-irrespective-of-activity-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!