I have a DialogDragment which I can show one of two ways:
1) By tapping on a ListView item from its OnItemClickListener
2) By activating a the ListView's context menu and selecting a menu item
Doing #1 works fine under all lifecycle events, but if I invoke it via #2 and I pause the activity (by going Home) and the resuming it via the task switcher, the dialog is no longer displayed. The fragment is there, and I can rotate the device and show the dialog.
I experimented, and if I put the showing of the DialogFragment into a Handler with a delay of at least 1/2 seconds, it works.
The following snippet fails -- it shows the dialog, but then pause/resume hides it:
public boolean onContextItemSelected(android.view.MenuItem item) {
boolean consumed = false;
switch (item.getItemId()) {
case R.id.menu_item:
showMyDialogFragment();
consumed = true;
break;
}
return consumed;
}
So the following snippet works. Pause/resume display the dialog again correctly:
public boolean onContextItemSelected(android.view.MenuItem item) {
boolean consumed = false;
switch (item.getItemId()) {
case R.id.menu_item:
new Handler().postDelayed(new Runnable() {
public void run() {
showMyDialogFragment();
}
}, 300);
consumed = true;
break;
}
return consumed;
}
Replacing the 300ms second delay with a 0ms or 250ms delay causes it to be broken again. This repeatable 100% of the time.
This is a terrible hack obviously, made worse by the constant that's probably depends on the speed of the device.
Anybody know why this is going on and/or offer a better solution? I spent hours on this issue and this is the best I could come up with.