Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing only one DialogFragment

I am trying to set up a dialog fragment that pops up when my android user receives a push notification. The code I have below triggers the dialog. The issue I am running into is that if my users get multiple pushes, they will see multiple dialog boxes popup.

My desired action is to show only one dialog box and if another one pops up before the current one is closed, the current one should be destroyed and then the new one 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 and show the dialog
      DialogFragment newFragment = ShiftsDialogFragment.newInstance(time);
      newFragment.show(getSupportFragmentManager().beginTransaction(), DIALOG_ALERT);
  }
}

I have tried using the code from the android docs (http://developer.android.com/reference/android/app/DialogFragment.html). When debugging, it looks like prev is always null.

From my understanding, it looks like I am attaching the DialogFragment to the SupportFragmentManager:

newFragment.show(getSupportFragmentManager().beginTransaction(), DIALOG_ALERT);

and when I try to check to see if there are any current DialogFragment, I am checking from the FragmentManager:

 android.app.Fragment prev = getFragmentManager().findFragmentByTag(DIALOG_ALERT);

If I try to change the code to try to get it from the SupportFragmentManager, I get an incompatible type error where it is expecting android.app.Fragment, but I am returning a android.support.v4.app.Fragment:

android.app.Fragment prev = getSupportFragmentManager().findFragmentByTag(DIALOG_ALERT);

How can I manage my DialogFragment so that only one is shown at any 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 and show the dialog
    DialogFragment newFragment = ShiftsDialogFragment.newInstance(time);
    newFragment.show(getSupportFragmentManager().beginTransaction(), DIALOG_ALERT);
}
like image 555
Huy Avatar asked Dec 02 '14 03:12

Huy


People also ask

How do I know if DialogFragment is showing?

Showing the DialogFragment Instead, use the show() method to display your dialog. You can pass a reference to a FragmentManager and a String to use as a FragmentTransaction tag.

What is the difference between dialog & DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.

Is DialogFragment deprecated?

This method is deprecated. androidx.

What is DialogFragment used for?

DialogFragment is a utility class which extends the Fragment class. It is a part of the v4 support library and is used to display an overlay modal window within an activity that floats on top of the rest of the content. Essentially a DialogFragment displays a Dialog but inside a Fragment.


1 Answers

Your issue seems to be the incompatibility of the DialogFragment. If ShiftsDialogFragment is a sub-class of android.support.v4.app.DialogFragment you can use

android.support.v4.app.Fragment prev = getSupportFragmentManager().findFragmentByTag(DIALOG_ALERT);
like image 75
Rajesh Avatar answered Sep 17 '22 08:09

Rajesh