Skip to content

Instantly share code, notes, and snippets.

@Ahmed-Abdelmeged
Created June 19, 2020 20:11
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 Ahmed-Abdelmeged/fffe75512ed7c0fec5cf053357ae8cb1 to your computer and use it in GitHub Desktop.
Save Ahmed-Abdelmeged/fffe75512ed7c0fec5cf053357ae8cb1 to your computer and use it in GitHub Desktop.
abstract class BaseActivity<VB : ViewBinding, C : Coordinator> : AppCompatActivity(),
CoordinatorHost<C> {
lateinit var binding: VB
private set
@get:NavigationRes
abstract val graph: Int
@get:IdRes
abstract val navHostId: Int
abstract fun onPerformInjection()
abstract fun onCreateBinding(inflater: LayoutInflater): VB
abstract fun onStartDestination(): StartDestination
override fun onCreate(savedInstanceState: Bundle?) {
onPerformInjection()
super.onCreate(savedInstanceState)
binding = onCreateBinding(layoutInflater)
setContentView(binding.root)
coordinator.navController = findNavController(navHostId)
coordinator.activity = this
setupNavigationGraph(
graphId = graph,
host = supportFragmentManager.findFragmentById(navHostId) as NavHostFragment,
startDestination = onStartDestination()
)
}
private fun setupNavigationGraph(
@NavigationRes graphId: Int,
host: NavHostFragment,
startDestination: StartDestination
) {
val inflater = host.navController.navInflater
val graph = inflater.inflate(graphId)
graph.startDestination = startDestination.destination
host.navController.setGraph(graph, startDestination.args)
}
override fun onSupportNavigateUp(): Boolean {
return findNavController(navHostId).navigateUp()
}
override fun onDestroy() {
super.onDestroy()
coordinator.activity = null
coordinator.navController = null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment