Skip to content

Commit

Permalink
Fix current fragment not removed on back press
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsvanvelzen committed Sep 3, 2024
1 parent e460c6e commit 13f6a70
Showing 1 changed file with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,19 @@ class DestinationFragmentView @JvmOverloads constructor(
// Require at least 2 items (current & previous) to go back
if (history.size < 2) return false

// Create the base transaction so we can mutate everything at once
val transaction = fragmentManager.beginTransaction()

// Remove current entry
history.pop()
val currentEntry = history.pop()

// Make sure to remove the associated fragment
val currentFragment = currentEntry.fragment
if (currentFragment != null) transaction.remove(currentFragment)

// Read & set previous entry
val entry = history.last()
activateHistoryEntry(entry)
activateHistoryEntry(entry, transaction)

return true
}
Expand All @@ -119,7 +126,7 @@ class DestinationFragmentView @JvmOverloads constructor(
@SuppressLint("CommitTransaction")
private fun activateHistoryEntry(
entry: HistoryEntry,
transaction: FragmentTransaction = fragmentManager.beginTransaction(),
transaction: FragmentTransaction,
) {
var fragment = entry.fragment

Expand Down Expand Up @@ -178,7 +185,7 @@ class DestinationFragmentView @JvmOverloads constructor(
if (savedHistory != null) {
history.clear()
history.addAll(savedHistory)
if (history.isNotEmpty()) activateHistoryEntry(history.last())
if (history.isNotEmpty()) activateHistoryEntry(history.last(), fragmentManager.beginTransaction())
}
}
}

0 comments on commit 13f6a70

Please sign in to comment.