问题
I have a DialogFragment
that was supposed to be simple but it's giving me some big problems specifically on Jelly Bean.
The app uses the network and it pops a dialogue asking the user to turn on WiFi or cancel then closes it. So it extends DialogFragment
and creates view as:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog a = new AlertDialog.Builder(getActivity()).setCancelable(true).setTitle(R.string.dialog_title_disabled)
.setMessage(R.string.dialog_text)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dismiss();
Intent i = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
}).setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
getActivity().finish();
}
}).create();
//a.setCanceledOnTouchOutside(false);
return a;
}
If the user clicks Yes, it dismisses the dialogue and opens the Wireless settings activity. Or if the user clicks Cancel it just closes my whole activity, but on Jelly Bean, anytime I click Yes, it does open the Settings, but the app force closes with the following error:
08-05 20:24:22.584: E/AndroidRuntime(2579): java.lang.IllegalStateException: Failure saving state: active SettingsDialogFragment{425dd550} has cleared index: -1
08-05 20:24:22.584: E/AndroidRuntime(2579): at android.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1653)
There's some extra logging showing the saved state of each fragment that was on my layout and the number 2 that was supposed to be the SettingsDialogFragment
is just a null
:
08-05 20:24:22.576: E/FragmentManager(2579): #2: null
I tried to not dismiss the dialogue but it crashed the same way.
I'm really not sure what's going on here… Any ideas?
EDIT:
The Activity code (it's a normal activity because the app targets ICS and up):
private void showDialog() {
SettingsDialogFragment diag = (SettingsDialogFragment) getFragmentManager().findFragmentByTag(DIALOG_TAG);
if (diag == null) {
diag = new SettingsDialogFragment();
diag.show(getFragmentManager(), DIALOG_TAG);
} else {
if (!diag.isVisible())
diag.show(getFragmentManager(), DIALOG_TAG);
}
}
private void dismissDialog() {
SettingsDialogFragment diag = (SettingsDialogFragment) getFragmentManager().findFragmentByTag(DIALOG_TAG);
if (diag != null)
diag.dismiss();
}
回答1:
Apparently Google changed something from ICS to JB and to dismiss the dialogue I had to use:
dismiss();
getFragmentManager().beginTransaction().remove(frag).commit();
It seems that the dialogFragment is not removing itself from the fragment manager OnDismiss like it used to, if someone cares to dig into the source-code and double check for the communitiy, it would be super.
thanks.
来源:https://stackoverflow.com/questions/11819460/dialogfragment-crashes-activity-when-calling-startactivity