How to Implement Firebase Deep Link with Navigation Graph and Safe Arguments
Learn how to implement Firebase Deep Link with Navigation Graph and Safe Arguments in your Android app. This tutorial will guide you through the process of setting up deep linking in your app using the latest navigation components and safe arguments.
Here is a step-by-step guide on how to implement Firebase Deep Link with Navigation Graph and Safe Arguments in your Android app:
Step 1: Add Firebase to your project
- Add Firebase to your project by following the Firebase setup guide.
- Configure Firebase Dynamic Links by adding the necessary details, such as your app's package name and SHA-1 certificate fingerprint.
Step 2: Configure Navigation Graph and Safe Arguments
- Create a new destination in your navigation graph for the deep link destination.
- Add the necessary arguments to the destination by creating a new Safe Args file or adding arguments manually.
Step 3: Handle Deep Links in Navigation Graph
Add the <nav-graph> tag to your activity in the AndroidManifest.xml file in your Android project
<activity
android:name="MainActivity"
android:exported="true"
android:screenOrientation="portrait"
android:theme="@style/AppTheme">
<nav-graph android:value="@navigation/nav_main" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
- By setting the <nav-graph> tag to this value, you are telling the activity to use this navigation graph to determine the flow of fragments within the app when this activity is launched.
- The <nav-graph> tag is used to specify the navigation graph file that defines the navigation flow for this activity. In this case, it is set to "@navigation/nav_main", which refers to the "nav_main.xml" file located in the "navigation" directory of your project's resources.
Now Add the deep link tag to your navigation graph
<fragment
android:id="@+id/fragment1"
android:name="Fragment1"
android:label="Fragment1">
<argument
android:name="param1"
app:argType="string" />
<deepLink
android:id="@+id/deepLink"
app:uri="https://example.com?arg1={param1}" />
</fragment>
Step 4: Test your implementation
- Test your implementation by creating a Firebase Dynamic Link and sharing it with your app.
- Verify that the app opens to the correct destination and that the appropriate data is passed through Safe Arguments.
That's it! With these steps, you should be able to implement Firebase Deep Link with Navigation Graph and Safe Arguments in your Android app.