If you're using the Android Jetpack Navigation library you can easily create deeplinks for fragments, which makes sense since the whole philosophy of the library is to have a single activity with multiple fragments. You can find the documentation here.
The gist of it is adding a deeplink
element in your nav_graph.xml
file like this:
<fragment android:id="@+id/a"
android:name="com.example.myapplication.FragmentA"
tools:layout="@layout/a">
<deeplink app:url="www.example.com"
app:action="android.intent.action.MY_ACTION"
app:mimeType="type/subtype"/>
</fragment>
And then linking the navigation XML to your manifest adding the following into your AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<application ... >
<activity name=".MainActivity" ...>
...
<nav-graph android:value="@navigation/nav_graph" />
...
</activity>
</application>
</manifest>
This will generate all the intent-filters
necessary to make use from the deeplinks.
Then you get into FragmentA
using intents, i.e.
val intent = Intent().apply {
data = Uri.parse("www.example.com")
}
Or if you're already in your app, you can use your NavController
and simply call findNavController().navigate("www.example.com")