devadnqpnd devadnqpnd - 2 months ago 36
Android Question

Fragment not attached to Activity in AlertDialog

Any idea why I'm having an illegal state exception with this code block?

mDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (isAdded()) {
Intent viewIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(viewIntent);
dialog.dismiss();
}
}
});


Is it because I'm dismissing the dialog after starting a new activity?

Answer
                    startActivity(viewIntent);
                    dialog.dismiss();

 replace with below 
           dialog.dismiss();
           startActivity(viewIntent);

Is it because I'm dismissing the dialog after starting a new activity?

Ans : Yes

Well Activity and Dialog are different.Activity has a Dialog so without activity dialogs will not have any stand.(Only activity as has control over UI not dialog).When you startNewactivity all activity state is saved activity stack but stack cannot dialogs states since its not part activity UI.(Dialog can exists only present activity context). If any dialogs shown by any activity, before finishing or gettting saved to activity stack dialogs has to be removed from the UI.