fix fragments cleanup on recreation

after back press if other MediaFragments where created via similar tab
This commit is contained in:
Jannik 2022-04-03 17:22:28 +02:00
parent 3a71bdd2c7
commit 7abb5cd3e8
Signed by: Seil0
GPG Key ID: E8459F3723C52C24
1 changed files with 12 additions and 8 deletions

View File

@ -129,8 +129,12 @@ class MediaFragment(private val mediaIdStr: String) : Fragment() {
val watchlistIcon = if (isWatchlist) R.drawable.ic_baseline_check_24 else R.drawable.ic_baseline_add_24
Glide.with(requireContext()).load(watchlistIcon).into(binding.imageMyListAction)
// clear fragments, since it lives in onCreate scope (don't do this in onPause/onStop -> FragmentManager transaction)
val fragmentsSize = if (fragments.lastIndex < 0) 0 else fragments.lastIndex
/**
* clear fragments, since it lives in onCreate scope,
* don't do this in onPause/onStop -> FragmentManager transaction
* (will be called on similar -> new MediaFragment -> onBackPressed)
*/
val fragmentsSize = fragments.size
fragments.clear()
pagerAdapter.notifyItemRangeRemoved(0, fragmentsSize)
@ -168,12 +172,12 @@ class MediaFragment(private val mediaIdStr: String) : Fragment() {
}
// if has similar titles
// if (model.similarTo.total > 0) {
// MediaFragmentSimilar().also {
// fragments.add(it)
// pagerAdapter.notifyItemInserted(fragments.indexOf(it))
// }
// }
if (model.similarTo.total > 0) {
MediaFragmentSimilar().also {
fragments.add(it)
pagerAdapter.notifyItemInserted(fragments.indexOf(it))
}
}
// disable scrolling on appbar, if no tabs where added
if(fragments.isEmpty()) {