android - Showing only one DialogFragment -
i trying set dialog fragment pops when android user receives push notification. code have below triggers dialog. issue running if users multiple pushes, see multiple dialog boxes popup.
my desired action show 1 dialog box , if 1 pops before current 1 closed, current 1 should destroyed , new 1 shown.
public abstract class baseactivity extends actionbaractivity { public void showshiftsdialog(string time) { string dialog_alert = "dialog_alert"; fragmenttransaction transaction = getfragmentmanager().begintransaction(); android.app.fragment prev = getfragmentmanager().findfragmentbytag(dialog_alert); if (prev != null) transaction.remove(prev); transaction.addtobackstack(null); // create , show dialog dialogfragment newfragment = shiftsdialogfragment.newinstance(time); newfragment.show(getsupportfragmentmanager().begintransaction(), dialog_alert); } }
i have tried using code android docs (http://developer.android.com/reference/android/app/dialogfragment.html). when debugging, looks prev
null.
from understanding, looks attaching dialogfragment supportfragmentmanager:
newfragment.show(getsupportfragmentmanager().begintransaction(), dialog_alert);
and when try check see if there current dialogfragment, checking fragmentmanager:
android.app.fragment prev = getfragmentmanager().findfragmentbytag(dialog_alert);
if try change code try supportfragmentmanager, incompatible type error expecting android.app.fragment
, returning android.support.v4.app.fragment
:
android.app.fragment prev = getsupportfragmentmanager().findfragmentbytag(dialog_alert);
how can manage dialogfragment 1 shown @ given time?
working solution
public void showshiftsdialog(string time) { string dialog_alert = "dialog_alert"; android.support.v4.app.fragmenttransaction transaction = getsupportfragmentmanager().begintransaction(); android.support.v4.app.fragment prev = getsupportfragmentmanager().findfragmentbytag(dialog_alert); if (prev != null){ dialogfragment df = (dialogfragment) prev; df.dismiss(); transaction.remove(prev); } transaction.addtobackstack(null); // create , show dialog dialogfragment newfragment = shiftsdialogfragment.newinstance(time); newfragment.show(getsupportfragmentmanager().begintransaction(), dialog_alert); }
your issue seems incompatibility of dialogfragment
. if shiftsdialogfragment
sub-class of android.support.v4.app.dialogfragment
can use
android.support.v4.app.fragment prev = getsupportfragmentmanager().findfragmentbytag(dialog_alert);
Comments
Post a Comment