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

前端 未结 9 1132
误落风尘
误落风尘 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 15:00

    I also faced a similar problem.
    I realized that this happened because the activity was being destroyed while the FragmentTransaction was about to get .commit().

    A solution to this was to check whether the Activity.isFinishing() is true or not.

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

提交回复
热议问题