android - What is the best practice of Global Dialog in my Application -
i have custom dialog builder in app trigger many events like: asynctasks, web service messages, ui input error or services don't have activity context. used create activity called currentactivity
in application class. on resume of every activities sit on currentactivity
.
@override protected void onresume() { super.onresume(); myapplication.currentactivity = mainactivity.this;
then in case of create dialogs used context. had 1 problem. example opened registeractivity , currentactivity
changed it. when app goto background or case other activity opened, app crash on creating dialog context. handling witch activity currentactivity
headache. googled , find guys embed customdialog in non-layout activty , open activity. seems not solution.
updated
for example have smsmanager class handle sending sms. wanna open dialog user chose custom options before sending sms.
so best practice of global dialog in whole application?
first of - bad practice save references activities (or context
in general). android provides reference context
object can use creating dialog. in activity
object (this
), in fragment
can access context
getactivity()
or getcontext()
, in view
- getcontext()
.
if need display dialog custom class , don't have reference context
make sure, provide context
reference class using methods described above.
don't try displaying dialogs service
- make sure app in foreground , visible, before showing dialogs. can use event bus (or localbroadcastmanager) sending state (error, message or whatever) visible activity
or fragment
. "current visible activity or fragment" in case activity
or fragment
listening such kind of events. start listening in onstart()
, stop listening in onstop()
methods of activity
or fragment
. if don't want rely on running activities displaying dialog , don't want wait next time user starts app, suggest using notifications instead of dialogs.
given context
can create custom dialog whereever want using helper dialog builder class this:
public class dialogbuilder { private string title; private string message; private string primarybuttontitle; private string secondarybuttontitle; private dialog.onclicklistener primarybuttonlistener; private dialog.onclicklistener secondarybuttonlistener; private activity context; private boolean showicon; private boolean cancellable; public dialogbuilder(activity context) { this.context = context; } public dialogbuilder settitle(@stringres int title) { this.title = context.getstring(title); return this; } public dialogbuilder settitle(string title) { this.title = title; return this; } public dialogbuilder setmessage(@stringres int message) { this.message = context.getstring(message); return this; } public dialogbuilder setmessage(string message) { this.message = message; return this; } public dialogbuilder setshowicon() { showicon = true; return this; } public dialogbuilder setprimarybutton(@stringres int title, dialog.onclicklistener listener) { primarybuttontitle = context.getstring(title); primarybuttonlistener = listener; return this; } public dialogbuilder setsecondarybutton(@stringres int title, dialog.onclicklistener listener) { secondarybuttontitle = context.getstring(title); secondarybuttonlistener = listener; return this; } public dialogbuilder setcancellable(boolean cancellable) { this.cancellable = cancellable; return this; } public alertdialog create() { alertdialog.builder builder = new alertdialog.builder(context); view dialogview = layoutinflater.from(context).inflate(r.layout.my_custom_dialog, null); builder.setview(dialogview); // custom views here , configure them based on given settings (field values of class) final alertdialog dialog = builder.create(); return dialog; } }
example usage (in fragment
):
new dialogbuilder(getactivity()) .settitle(r.string.title) .setmessage(r.string.message) .setprimarybutton(r.string.ok, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { // dialog.dismiss(); } }) .setsecondarybutton(r.string.settings_cancel, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { // dialog.dismiss(); } }).create().show();
Comments
Post a Comment