Skip to content

Instantly share code, notes, and snippets.

@NezSpencer
Created October 7, 2018 10:59
Show Gist options
  • Save NezSpencer/0cf11a3bd4e11370f9a28025f7118129 to your computer and use it in GitHub Desktop.
Save NezSpencer/0cf11a3bd4e11370f9a28025f7118129 to your computer and use it in GitHub Desktop.
pop backstack to a known fragment
private fun popToKnownFragment(fragment : Fragment) {
if (!lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED))
return
val isPopped = supportFragmentManager.popBackStackImmediate(fragment::class.simpleName, 0)
if (!isPopped) {
// code inside here is only executed because fragment transaction didnt happen because
// the fragment above does not exist in the backstack or it was added with a different tag.
// Since this fragment must be shown, add a new instance of it
supportFragmentManager.beginTransaction()
.replace(R.id.app_container, fragment, fragment::class.simpleName)
.addToBackStack(fragment::class.simpleName)
.commit()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment