Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent Dialog (or DialogFragment) from closing when app goes to background

It's pretty common for my app to show a progress or AlertDialog to the user. If the user puts the app into the background and then returns later, I want the Dialog to still be shown. Is there a way to make Android handle this? I'd like it to either not close the dialog, or if it does reopen it automatically when the Activity resumes.

So far it's looking like no. I haven't found a ton of results about this (most people run into issues with orientation change, which my app does not allow) but very few ask about going into the background. I have tried every permutation of DialogFragment and regular Dialog, but they all disappear when the home button is pressed and the app is opened from the task manager.

I don't even have any code to show because it's all in the testing phase of various examples online. I suspect I will have to manage this myself, by checking in onResume() if something should be shown. If this is the case I can live with it, but I'd like to know for sure.

like image 654
Mark Herscher Avatar asked Apr 28 '15 22:04

Mark Herscher


People also ask

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?

Stay organized with collections Save and categorize content based on your preferences. This class was deprecated in API level 28.

How do you dismiss dialog with click on outside of dialog?

By default, dialog can be closed by pressing Esc key and clicking the close icon on the right of dialog header. It can also be closed by clicking outside of the dialog using hide method. Set the CloseOnEscape property value to false to prevent closing of the dialog when pressing Esc key.

What is DialogFragment?

Android DialogFragments. 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

First lets clear something, like you can see in the next images, your activity or fragment can be destroyed for many reasons, so you have to deal with what you want saving "the state of your dialog".

activity life cycleenter image description here

Now the code:

public class CustomProgressDialog extends Dialog {

    private static final String SHOWING_PROGRESS_DIALOG = "showing_progress_dialog";
    private static final String STRING_PROGRESS_DIALOG = "string_progress_dialog";
    private static final String SHOWING_POP_UP_DIALOG = "showing_pop_up_dialog";
    private static final String STRING_POP_UP_DIALOG = "string_pop_up_dialog";

    public TextView textView;

    public CustomProgressDialog(Context context) {
        super(context, android.R.style.Theme_Translucent_NoTitleBar);

        setContentView(R.layout.progress_layout);

        setCancelable(false);

        textView = (TextView) findViewById(R.id.progress_textView);
    }

}


public class MasterActivity extends FragmentActivity {
    private CustomProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_master);

        progressDialog = new CustomProgressDialog(this);

        if (savedInstanceState != null) {
            boolean showingDialog = savedInstanceState.getBoolean(SHOWING_PROGRESS_DIALOG);
            if (showingDialog) {
                String msg = savedInstanceState.getString(STRING_PROGRESS_DIALOG, getResources().getString(R.string.progress_default_text));
                progressDialog.textView.setText(msg);
                progressDialog.show();
            }

            boolean mShowing_PopUpdialog = savedInstanceState.getBoolean(SHOWING_POP_UP_DIALOG);
            String temp_msg = savedInstanceState.getString(STRING_POP_UP_DIALOG, "");

            if (mShowing_PopUpdialog)
                showPopUpDialog(temp_msg);
            }
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        if (progressDialog.isShowing()) {
            outState.putBoolean(SHOWING_PROGRESS_DIALOG, true);
            outState.putString(STRING_PROGRESS_DIALOG, progressDialog.textView.getText().toString());
        }

        if (alert != null && alert.isShowing()) {
            outState.putBoolean(SHOWING_POP_UP_DIALOG, true);
            outState.putString(STRING_POP_UP_DIALOG, mString_dialog);
        }
    }
}
like image 195
MiguelHincapieC Avatar answered Sep 22 '22 16:09

MiguelHincapieC