Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close a dialogfragment after rotating the screen

I have implemented a dialogFragment which shows a simple message:

private static DialogFragment loadingDialogFragment = null;

public void showLoadingDialog(String title, String message) {
    loadingDialogFragment = new LoadingDialogFragment();
    Bundle args = new Bundle();
    args.putString("title", title);
    args.putString("message", message);
    loadingDialogFragment.setArguments(args);
    loadingDialogFragment.show(fragManager, "loadingDialog");
}

public void dismissLoadingDialog() {
    if (loadingDialogFragment != null) {
        loadingDialogFragment.dismiss();
    }
}

However if the dialogue is shown and I rotate the screen, then my dissmisslaodingDialog doesn't work. I am thinking this is because when you rotate it calls the on create function again and it loosing the reference to the dialog, but i have made it static.

The code above is put in a Utility class which I call from my activity like so:

util.showLoadingDialog("Loading", "");
SendCommand(deviceId, "Command");   

I then call utils.dismissLoadingDialog in the callback function of the SendCommmand.

What am I missing here?

{EDIT}

The code in my activityFragment

util.showLoadingDialog("Loading", "");
SendCommand(deviceId, "Command");

public void SendCommand(int deviceId, final String command) {

        Network.get(mContext, "/" + command + "/" + deviceId, null, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                Log.i(TAG + " sendPanelCommand", "recieved ok response to command: " + command);                
                util.dismissLoadingDialog();
            }

             @Override
             public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                util.dismissLoadingDialog();                    
                util.showErrorDialog(
                        "Error",
                        "An error has occored while trying to issue the command to the panel. This can be caused by network issues or your panel sending out priority information such as alarms. Please try again.");
            }

        });
    }
}

Utils Class:

public class Utils {

private Context context;
private FragmentManager fragManager;

public Utils(Context context) {
    this.context = context;
}

public Utils(Context context, FragmentManager fragman) {
    this.context = context;
    this.fragManager = fragman;
}
...

private DialogFragment loadingDialogFragment = null;

public void showLoadingDialog(String title, String message) {
    loadingDialogFragment = new LoadingDialogFragment();
    Bundle args = new Bundle();
    args.putString("title", title);
    args.putString("message", message);
    loadingDialogFragment.setArguments(args);
    loadingDialogFragment.show(fragManager, "loadingDialog");
}

public void dismissLoadingDialog() {
    if (loadingDialogFragment != null) {
        loadingDialogFragment.dismiss();
    } else {
        // the reference is null, try the FragmentManager to see if it
        // wasn't
        // automatically restored
        DialogFragment dg = (DialogFragment) fragManager.findFragmentByTag("loadingDialog");
        if (dg != null) {
            // this reference isn't null so the dialog is available
            dg.dismiss();
        }
    }
}   
}
like image 785
Zapnologica Avatar asked Apr 30 '14 09:04

Zapnologica


People also ask

How do I close DialogFragment on Android?

tl;dr: The correct way to close a DialogFragment is to use dismiss() directly on the DialogFragment. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog.

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.


1 Answers

Cancel a DialogFragment on screen rotation. What I found out a few minutes ago after searching and trying all the variations for dismissing and handling rotation was the onPause method of the DialogFragment gets called during the rotation and you can dismiss it there.

public static class ErrorDialogFragment extends DialogFragment {
    // Global field to contain the error dialog
    private Dialog mDialog;

    public void onPause(){
        super.onPause();
        this.dismissAllowingStateLoss();
    }

    static ErrorDialogFragment newInstance() {
        ErrorDialogFragment d = new ErrorDialogFragment();
        return d;
    }
 ...
}
like image 135
danny117 Avatar answered Sep 20 '22 21:09

danny117