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.
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".
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);
}
}
}