I will demonstrate how to use deep link in Navigation Component on a big project and show some problems you might found during the implementation.
<?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/navigation_listing"
app:startDestination="@id/listingFragment">
<fragment
android:id="@+id/listingFragment"
android:name="com.him.oozoublog.navcom.listing.ui.listing.ListingFragment"
android:label="fragment_listing"
tools:layout="@layout/fragment_listing">
<deepLink
android:id="@+id/deepLink"
app:uri="myapp://listing" />
</fragment>
......
</navigation>
<?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"
android:id="@+id/navigation_shopping_cart"
app:startDestination="@id/shoppingCartFragment">
<fragment
android:id="@+id/shoppingCartFragment"
android:name="com.him.oozoublog.navcom.shoppingcart.ui.ShoppingCartFragment"
android:label="ShoppingCartFragment">
<deepLink
android:id="@+id/deepLink"
app:uri="myapp://shopping-cart" />
</fragment>
</navigation>
<?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"
android:id="@+id/history_navigation"
app:startDestination="@id/historyFragment">
<fragment
android:id="@+id/historyFragment"
android:name="com.him.oozoublog.navcom.history.ui.HistoryFragment"
android:label="HistoryFragment">
<deepLink
android:id="@+id/deepLink"
app:uri="myapp://history" />
</fragment>
</navigation>
<?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"
android:id="@+id/navigation_payment"
app:startDestination="@id/paymentFragment">
<fragment
android:id="@+id/paymentFragment"
android:name="com.him.oozoublog.navcom.payment.ui.PaymentFragment"
android:label="PaymentFragment">
<deepLink
android:id="@+id/deepLink"
app:uri="myapp://payment" />
</fragment>
</navigation>
<?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"
android:id="@+id/main_navigation"
app:startDestination="@+id/navigation_listing">
<include app:graph="@navigation/navigation_listing" />
<include app:graph="@navigation/navigation_history" />
<include app:graph="@navigation/navigation_shopping_cart" />
<include app:graph="@navigation/navigation_payment" />
</navigation>
object InternalDeepLink {
const val DOMAIN = "myapp://"
const val LISTING = "${DOMAIN}listing"
const val PAYMENT = "${DOMAIN}payment"
const val SHOPPING_CART = "${DOMAIN}shopping-cart"
const val HISTORY = "${DOMAIN}history"
fun makeCustomDeepLink(id: String): String {
return "${DOMAIN}customDeepLink?id=${id}"
}
}
listingViewModel._navigateToShoppingCart.observe(viewLifecycleOwner, {
val deepLink = InternalDeepLink.SHOPPING_CART.toUri()
findNavController().navigate(deepLink)
})
enum class SearchType {
ALL,
CAR,
WATCH
}
<?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/navigation_listing"
app:startDestination="@id/listingFragment">
<fragment
android:id="@+id/listingFragment"
android:name="com.him.oozoublog.navcom.listing.ui.listing.ListingFragment"
android:label="fragment_listing"
tools:layout="@layout/fragment_listing">
<argument
android:name="searchType"
android:defaultValue="ALL"
app:argType="com.him.oozoublog.navcom.listing.ui.listing.SearchType" />
<deepLink
android:id="@+id/deepLink"
app:uri="myapp://listing?searchType={searchType}" />
</fragment>
</navigation>
class ListingFragment : Fragment() {
....
private val args: ListingFragmentArgs by navArgs()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.i("search type", args.searchType.name)
}
....
}
From us to your inbox weekly.
1. Open fragment A with deep-link (press back -> close app)
2. Open fragment B with deep-link (press back -> close app)