Very simple code, but got error “Activity has been destroyed” when use Fragment

前端 未结 9 1154
误落风尘
误落风尘 2020-12-05 14:15

I made a very simple Activity which shows a simple ListFragment like below:

My Activity:

public class MyActivity ex         


        
9条回答
  •  庸人自扰
    2020-12-05 14:38

    super.onCreate(savedInstanceState) will call onCreate method in FragmentActivity, which will call mFragments.attachHost(null /*parent*/);

    This sentence will assign a value to mHost in FragmentController, when you calls FragmentTransaction.commit() ,

    In the method enqueueAction(),it has this sentences

     if (mDestroyed || mHost == null) {
            throw new IllegalStateException("Activity has been destroyed");
     }
    

    So if you haven't call super.onCreate(savedInstanceState) before you commit a fragmentTransaction, you will get this exception because mHost is null

提交回复
热议问题