getting exception “IllegalStateException: Can not perform this action after onSaveInstanceState”

前端 未结 30 2588
迷失自我
迷失自我 2020-11-22 05:12

I have a Live Android application, and from market i have received following stack trace and i have no idea why its happening as its not happening in application code but it

30条回答
  •  渐次进展
    2020-11-22 05:58

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

提交回复
热议问题