Skip to content

Instantly share code, notes, and snippets.

@prongbang
Created November 28, 2021 17:05
Show Gist options
  • Save prongbang/7d859d976df8beb8d887c5606b01a5f6 to your computer and use it in GitHub Desktop.
Save prongbang/7d859d976df8beb8d887c5606b01a5f6 to your computer and use it in GitHub Desktop.
How to make a dynamic start destination in the Android Navigation Component
<?xml version="1.0" encoding="utf-8"?>
<androidx.fragment.app.FragmentContainerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/navigationHostFragmentMain"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true" />
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ndid_navigation"
app:startDestination="@id/ndidFragment">
<fragment
android:id="@+id/ndidFragment"
android:name="com.prongbang.ndid.presentation.main.NdidFragment"
android:label="NdidFragment"
tools:layout="@layout/fragment_ndid"/>
<fragment
android:id="@+id/ndidFaceScanFragment"
android:name="com.prongbang.ndid.presentation.verify.NdidFaceScanFragment"
android:label="NdidFaceScanFragment"
tools:layout="@layout/fragment_ndid_face_scan">
<argument
android:name="argsName"
android:defaultValue=""
app:argType="string" />
</fragment>
</navigation>
class MainActivity : AppCompatActivity {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
initNavGraphStartDestination()
}
private fun initNavGraphStartDestination() {
setupNavGraphStartDestination(
fragmentContainerViewIdRes = R.id.navigationHostFragmentMain,
navigationGraphRes = R.navigation.main_nav_graph,
startDestinationIdRes = R.id.ndidFaceScanFragment,
arguments = arrayOf(
NavArguments(
key = "argsName",
value = "NDID-FACE-SCAN"
)
),
)
}
}
import androidx.annotation.IdRes
import androidx.annotation.NavigationRes
import androidx.fragment.app.FragmentActivity
import androidx.navigation.NavArgument
import androidx.navigation.fragment.NavHostFragment
data class NavArguments(
val key: String,
val value: Any?
)
fun FragmentActivity.configNavStartDestination(
@IdRes fragmentContainerViewIdRes: Int,
@NavigationRes navigationGraphRes: Int,
@IdRes startDestinationIdRes: Int,
vararg arguments: NavArguments = emptyArray()
) {
val navHostFragment =
supportFragmentManager.findFragmentById(fragmentContainerViewIdRes) as? NavHostFragment
navHostFragment?.apply {
val graphInflater = navHostFragment.navController.navInflater
val navController = navHostFragment.navController
val navGraph = graphInflater.inflate(navigationGraphRes)
navController.graph = navGraph.apply {
startDestination = startDestinationIdRes
for (arg in arguments) {
addArgument(arg.key, NavArgument.Builder().setDefaultValue(arg.value).build())
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment