8

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.

Elltz
  • 10,730
  • 4
  • 31
  • 59
Mark Herscher
  • 1,761
  • 2
  • 19
  • 32

3 Answers3

4

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 cycle enter 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);
        }
    }
}
MiguelHincapieC
  • 5,445
  • 7
  • 41
  • 72
0

Try this in your DialogFragment's onPause() do this

@Override   
public void onPause() {
super.onPause();
getActivity().getSupportFragmentManager().beginTransaction()
               .addToBackStack("mydialogfragment").commit();
Log.v("dialog", "dialog is going down");
}

Then in your Activity's onResume() you call the DialogFragment back to life

@Override
protected void onResume() {
    super.onResume();
    getSupportFragmentManager().popBackStack();
    Log.v("activity", "onresume called - i am bringing back the dialog");
}

Why i think this might work is a DialogFragment is a Fragment, and a Fragment's lifecycle is controlled by the Parent Activity as user @0mach0 diagram shows, so all you do is you push it do the backstack and call it back. so it should work

Elltz
  • 10,730
  • 4
  • 31
  • 59
0

Try showing the dialog using parentFragmentManager. Worked for my DialogFragment

.show(parentFragmentManager, "TAG")

Savvasenok
  • 31
  • 6