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

后端 未结 7 671
北海茫月
北海茫月 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:21

    this worked for me... found this out on my own... hope it helps you!

    1) do NOT have a global "static" FragmentManager / FragmentTransaction.

    2) onCreate, ALWAYS initialize the FragmentManager again!

    sample below :-

    public abstract class FragmentController extends AnotherActivity{
    protected FragmentManager fragmentManager;
    protected FragmentTransaction fragmentTransaction;
    protected Bundle mSavedInstanceState;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSavedInstanceState = savedInstanceState;
        setDefaultFragments();
    }
    
    protected void setDefaultFragments() {
        fragmentManager = getSupportFragmentManager();
        //check if on orientation change.. do not re-add fragments!
        if(mSavedInstanceState == null) {
            //instantiate the fragment manager
    
            fragmentTransaction = fragmentManager.beginTransaction();
    
            //the navigation fragments
            NavigationFragment navFrag = new NavigationFragment();
            ToolbarFragment toolFrag = new ToolbarFragment();
    
            fragmentTransaction.add(R.id.NavLayout, navFrag, "NavFrag");
            fragmentTransaction.add(R.id.ToolbarLayout, toolFrag, "ToolFrag");
            fragmentTransaction.commitAllowingStateLoss();
    
            //add own fragment to the nav (abstract method)
            setOwnFragment();
        }
    }
    

提交回复
热议问题