显示 DialogFragment 的应用在旋转后崩溃

App showing DialogFragment crashes after rotation

当我在显示 DialogFragment 的同时旋转我的设备时,我的应用程序崩溃了:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapp, PID: 27909
java.lang.RuntimeException: Unable to destroy activity {com.myapp/com.myapp.FileListActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4203)
    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4221)
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4495)
    at android.app.ActivityThread.-wrap19(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1483)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6126)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
    Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
    at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1493)
    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1511)
    at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:638)
    at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:617)
    at android.support.v4.app.DialogFragment.dismissInternal(DialogFragment.java:201)
    at android.support.v4.app.DialogFragment.dismiss(DialogFragment.java:167)
    at com.myapp.ActionsDialogFragment.onDestroy(ActionsDialogFragment.java:127)
    at android.support.v4.app.Fragment.performDestroy(Fragment.java:2202)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1196)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1234)
    at android.support.v4.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:2083)
    at android.support.v4.app.FragmentController.dispatchDestroy(FragmentController.java:244)
    at android.support.v4.app.FragmentActivity.onDestroy(FragmentActivity.java:368)
    at android.support.v7.app.AppCompatActivity.onDestroy(AppCompatActivity.java:203)
    at com.myapp.FileListActivity.onDestroy(FileListActivity.java:1106)
    at android.app.Activity.performDestroy(Activity.java:6881)
    at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1154)
    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4190)
    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4221) 
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4495) 
    at android.app.ActivityThread.-wrap19(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1483) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6126) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

那是我的 DialogFragment:

public class ActionsDialogFragment extends DialogFragment {

    public interface ActionsDialogListener {
        void onActionSelected(DialogFragment dialog, int position);
    }

    AlertDialog dialog;
    ActionsDialogListener mListener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (ActionsDialogListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement ActionsDialogListener");
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        dialog = builder.create();
        return dialog;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.actions_dialog, container, false);

        CustomListAdapter cla;
        cla = new CustomListAdapter(getActivity());
        ListView list = (ListView) view.findViewById(R.id.listViewActions);
        final AlertDialog finalDialog = dialog;
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mListener.onActionSelected((DialogFragment) getParentFragment(), position);
                Log.d(FileListActivity.TAG, "Dismissing dialog");
//                ActionsDialogFragment.this.dismiss();
                finalDialog.dismiss();
            }
        });
        list.setAdapter(cla);
        dialog.setView(view);
        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(FileListActivity.TAG, "ActionDialogFragment onDestroy()");
        dismiss();
//        dialog.dismiss();
    }

    class CustomListAdapter extends ArrayAdapter<String> {
    ...
    }

}

错误显示 "Can not perform this action after onSaveInstanceState"。但是我不知道 InstanceState 保存在哪里。对我来说,关闭对话框并在旋转后重新创建它是可以的。

知道出了什么问题吗?

编辑:

我的 DialogFragment 是在主 activity 中创建的,如下所示:

void showActionsDialog() {
    actionsDialog = new ActionsDialogFragment();
    actionsDialog.show(getSupportFragmentManager(), "ActionsDialogFragment");
}

编辑 2:

以防万一它很重要。这是我的主要 activity 的 onDestroy() 方法:

public void onDestroy() {
    super.onDestroy();
    Log.d(TAG, "FileListActivity onDestroy()");
    if (exifReadingDialog != null) { exifReadingDialog.dismiss(); }
    dataFragment.mRetainedCache = mMemoryCache;
    dataFragment.setData(mediaFolder);
}

编辑 3:

一些额外的想法:Whosebug 的许多人说你应该在 DialogFragment 中使用 onCreateDialogonCreateView。也许那是我的问题。但是,如果我删除 onCreateDialogdialog 变量,则关闭不再起作用。到目前为止,我正在使用这个解决方案:Cannot get DialogFragment to dismiss programatically(只是为了关闭对话框,但在旋转时崩溃)

我认为问题在于 onDestroy 调用父级 activity 上的片段事务提交,这不能在调用 onSavedInstance 之后完成,而这是在 onDestroy 之前完成的。你真的需要关闭 onDestroy() 吗??

你得到 Leaked Window 异常,因为 Alert dialog 是 运行 而你的 activity 被销毁了。你应该在 activity 被销毁时关闭对话框

试试这个:

@Override
protected void onDestroy() {
    try {
        if (dialog!= null && dialog.isShowing()) {
            dialog.dismiss();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    super.onDestroy();
}

同样在您的 setOnItemClickListener 中先关闭对话框

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            if (finalDialog != null && finalDialog.isShowing()) {
                  finalDialog.dismiss();
             }
            mListener.onActionSelected((DialogFragment) getParentFragment(), position);
            Log.d(FileListActivity.TAG, "Dismissing dialog");
    //      ActionsDialogFragment.this.dismiss();
        }
    });

同时我尝试了很多东西。我将 ActionsDialogFragment 更改为看起来更像 this tutorial

但我认为真正的错误在于我的习惯 ListAdapter。我改变了这部分

public @NonNull View getView(int position, View view, @NonNull ViewGroup parent) {
    LayoutInflater inflater = getLayoutInflater(null);

public @NonNull View getView(int position, View view, @NonNull ViewGroup parent) {
    LayoutInflater inflater = ((Activity)context).getLayoutInflater();

现在我可以通过一个简单的 dismiss(); 关闭 DialogFragment 并且应用程序在旋转后不会再崩溃。

所以我应该在我的问题中发布完整的 class。无论如何感谢您的帮助!