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

前端 未结 9 1142
误落风尘
误落风尘 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:40

    Still bad, because got error “Activity has been destroyed”

    ava.lang.IllegalStateException: Activity has been destroyed fragmentTransaction.commitAllowingStateLoss();
    

    So my solution is add check if (!isFinishing()&&!isDestroyed())

     if (!isFinishing()&&!isDestroyed()) {
                            fragmentTransaction.commitAllowingStateLoss();
                        }
                    }
    

提交回复
热议问题