Created
October 7, 2018 10:59
-
-
Save NezSpencer/0cf11a3bd4e11370f9a28025f7118129 to your computer and use it in GitHub Desktop.
pop backstack to a known fragment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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