“Can not perform this action after onSaveInstanceState” - why am I getting this exception from my activity's onResume method?

后端 未结 7 670
北海茫月
北海茫月 2020-12-08 02:25

My activity invokes the camera with the ACTION_IMAGE_CAPTURE intent. If the camera activity returns succesfully, I set a flag in the onActivityResult callback, and based on

7条回答
  •  借酒劲吻你
    2020-12-08 03:17

    I was always getting this when I tried to show fragment in onActivityForResult() method, so the problem was next:

    1. My Activity is paused and stopped, which means, that onSaveInstanceState() was called already (for both pre-Honeycomb and post-Honeycomb devices).
    2. In case of any result I made transaction to show/hide fragment, which causes this IllegalStateException.

    What I made is next:

    1. Added value for determining if action I want was done (e.g. taking photo from camere - isPhotoTaken) - it can be boolean or integer value depending how much different transactions you need.
    2. In overriden onResumeFragments() method I checked for my value and after made fragment transactions I needed. In this case commit() was not done after onSaveInstanceState, as state was returned in onResumeFragments() method.

提交回复
热议问题