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

3

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.

Clergyman answered 28/4, 2015 at 22:11 Comment(6)
have u tried with method: onSaveInstanceState? and restore it in OnCreate?Vedda
I can give you a snippet of code of an app of mine that do what are you looking for (using onSaveInstanceState method), should I post it?Vedda
? I do not know the answer.Clergyman
And yes, your onSaveInstanceState() example would be very helpful.Clergyman
@Elltz What are you talking about?Disadvantage
Guess he was talking to me :)Vedda
V
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);
        }
    }
}
Vedda answered 28/4, 2015 at 22:34 Comment(3)
In the code above I'm handle a PopUp too, guess you can just delete the lines about itVedda
Another thing you should take care is throwing behaviors while your app is in background (a return from a asyncTask that show a dialog, show another fragment/activity, etc), if you need just tell meVedda
Thanks! I'm now using something similar to this.Clergyman
B
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

Bicarbonate answered 28/4, 2015 at 23:19 Comment(0)
Z
0

Try showing the dialog using parentFragmentManager. Worked for my DialogFragment

.show(parentFragmentManager, "TAG")

Zilla answered 1/2, 2023 at 11:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.