IllegalStateException: Can not perform this action after onSaveInstanceState with onActivityResult

若如初见. 提交于 2019-12-02 11:52:39
ianhanniballake

You must call super.onActivityResult(requestCode, resultCode, data) before doing any FragmentTransactions in your onActivityResult() method as that call is what 'unlocks' the FragmentManager and notes that you are in a valid state to do FragmentTransactions.

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Add this line
        super.onActivityResult(requestCode, resultCode, data);

        // This all remains the same
        if (requestCode == 1) {
            if(resultCode == RESULT_OK) {
                MyCustomDialogFragment newPopup = new MyCustomDialogFragment();
                newPopup.setMyClickListener(MainActivity.this);
                FragmentManager fragmentManager = getSupportFragmentManager();
                newPopup.show(fragmentManager, "CashReceivePopup");  
          } 
       }
   }

It is a common problem. You have 2 options how to deal with this issue.

  1. you can override the show() method and put its content inside a try-catch block
  2. You display the dialog in the followong way

     FragmentManager fm = getSupportFragmentManager();
     MyDialog d = new MyDialog();
     FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
     ft.add(d, "dialog");
     ft.commitAllowingStateLoss();
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!