This is because each time you rotate the screen, your activity is recreated i.e., onCreate()
is called again. Since you are creating the fragment in onCreate
, a new fragment is created each time and the previous one is not destroyed.
To counter this, you can save the state of the activity using savedInstanceState
, maybe save a boolean that the fragment has been created once, and then check it in onCreate
whether to create the fragment or not.