java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState with DialogFragment

后端 未结 10 1144
孤独总比滥情好
孤独总比滥情好 2020-12-09 01:56

I am facing issue with DialogFragment / getSupportFragmentManager / Android version 4.x

01-10 19:46:48.228: E/AndroidRuntime(9879): java.lang.IllegalStateExc         


        
相关标签:
10条回答
  • 2020-12-09 02:24

    onPostResume() use post resume method to do your work.. I think you are calling show dialog method in onRestart or onResume, so avoid it and use onPostResume() to show your dialog.

    0 讨论(0)
  • 2020-12-09 02:28

    Here is the answer in another thread:

    Actions in onActivityResult and "Error Can not perform this action after onSaveInstanceState"

    also here:

    Refreshing my fragment not working like I thought it should

    This is an example solving this problem:

    DialogFragment loadingDialog = createDialog();
    
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                        transaction.add(loadingDialog, "loading");
                        transaction.commitAllowingStateLoss();  
    
    0 讨论(0)
  • 2020-12-09 02:33

    You could check if the current activity isActive() and only in that case start the fragment transaction of DialogFragment. I had a similar problem and this check solved my case.

    0 讨论(0)
  • 2020-12-09 02:34
    fragmentView.post(() -> {
        FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
        YourDialog yourDialog = YourDialog.newInstance();
        yourDialog.show(ft, "text_data");
    });
    

    The goal of post() method in this case is to wait while onResume() of Activity or Fragment will finished. It works if you want to show DialogFragment from Fragment., f.e. when you want to show your dialog after a system dialog dismissed.

    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题