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

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

    I faced the same issue and unable to fix it. Event I added isFinishing() check as well. but no luck.

    then I added one more check isDestroyed() and its working fine now.

    if (!isFinishing() && !isDestroyed()) {
      FragmentTransaction ft = getSupportFragmentManager()
         .beginTransaction();
      ft.replace(LAYOUT_ID, myFragmentInstance);
      ft.commit();
    }
    

提交回复
热议问题