当前位置: 首页 > 知识库问答 >
问题:

IllegalStateException:在带有onActivityResult的onSaveInstanceState之后无法执行此操作

陶成济
2023-03-14

我是Android开发的新手。我有个问题。我试了几个小时,但我想不出来。如果是这样的话,我有一个很受欢迎的问题。IllegalStateException:在使用ViewPager onSaveInstanceState之后无法执行此操作,但由于缺乏Android开发经验而失败。

下面是代码:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1) {
            if(resultCode == RESULT_OK) {
                MyCustomDialogFragment newPopup = new MyCustomDialogFragment();
                newPopup.setMyClickListener(MainActivity.this);
                FragmentManager fragmentManager = getSupportFragmentManager();
                newPopup.show(fragmentManager, "CashReceivePopup");  
          } 
       }
   }

共有1个答案

郜琦
2023-03-14

onActivityResult()方法中执行任何FragmentTransactions之前,必须调用super.onActivityResult(requestCode,resultCode,data),因为该调用将“解锁”FragmentManager,并注意到您处于执行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");  
          } 
       }
   }
 类似资料: