-
-
Save Ahmed-Abdelmeged/fffe75512ed7c0fec5cf053357ae8cb1 to your computer and use it in GitHub Desktop.
This file contains 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
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