Skip to content

Instantly share code, notes, and snippets.

@youngam
Last active September 27, 2017 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save youngam/2f9dc59909231d277536ee2d4fa174ed to your computer and use it in GitHub Desktop.
Save youngam/2f9dc59909231d277536ee2d4fa174ed to your computer and use it in GitHub Desktop.
class MainActivity : AppCompatActivity() {
private lateinit var fragmentContainer: ViewGroup
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
fragmentContainer = findViewById(R.id.fragment_container) as ViewGroup
}
public fun navigate(from: Fragment? = null, to: Fragment, addToBackStack: Boolean = true) {
if(from != null) from.userVisibleHint = false
val transaction = supportFragmentManager.beginTransaction()
.add(fragmentContainer.id, to)
if(addToBackStack) transaction.addToBackStack(null)
transaction.commit()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment