IllegalStateException: Can not perform this action after onSaveInstanceState with onActivityResult

人盡茶涼 提交于 2019-12-02 20:53:09

问题


I am new to Android development. I got an issue. I tried the last couple of hour but I can't figure out this. if so I got a popular question. IllegalStateException: Can not perform this action after onSaveInstanceState with ViewPager but failed because of the lack of Android development experience.

Here is code:

    @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");  
          } 
       }
   }

Here is the error:

01-04 05:08:57.010 13609-13609/com.nazmul.aznazgame.bitlife D/AndroidRuntime: Shutting down VM 01-04 05:08:57.068 13609-13609/com.nazmul.aznazgame.bitlife E/AndroidRuntime: FATAL EXCEPTION: main Process: com.nazmul.aznazgame.bitlife, PID: 13609 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=4, result=-1, data=Intent { (has extras) }} to activity {com.nazmul.aznazgame.bitlife/com.nazmul.aznazgame.bitlife.MainActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState at android.app.ActivityThread.deliverResults(ActivityThread.java:3574) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617) at android.app.ActivityThread.access$1300(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:2053) at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:2079) at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:678) at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:632) at android.support.v4.app.DialogFragment.show(DialogFragment.java:143) at com.nazmul.aznazgame.bitlife.MainActivity.showEducationDialog(MainActivity.java:1716) at com.nazmul.aznazgame.bitlife.MainActivity.manageActivity(MainActivity.java:604) at com.nazmul.aznazgame.bitlife.MainActivity.onActivityResult(MainActivity.java:580) at android.app.Activity.dispatchActivityResult(Activity.java:6192) at android.app.ActivityThread.deliverResults(ActivityThread.java:3570) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617)  at android.app.ActivityThread.access$1300(ActivityThread.java:151)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5254)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)  01-04 05:08:59.825 13609-13651/com.nazmul.aznazgame.bitlife I/CrashlyticsCore: Crashlytics report upload complete: 5C2EEA4F018E-0001-3529-63E978D09744 01-04 05:08:59.955 13609-13609/com.nazmul.aznazgame.bitlife I/Process: Sending signal. PID: 13609 SIG: 9


回答1:


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");  
          } 
       }
   }



回答2:


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();
    


来源:https://stackoverflow.com/questions/54033509/illegalstateexception-can-not-perform-this-action-after-onsaveinstancestate-wit

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