DialogFragment crashes Activity when calling startActivity()

£可爱£侵袭症+ 提交于 2020-01-01 05:42:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!