How do Deep Link into a fragment in an Android App. Is it possible?
Asked Answered
N

3

18

So is it possible to deep link into a fragment? So my main activity launches different fragments depending on what the user clicks.

So i created a deep link for my main activity with the intent filter in the manifest file. But how would you do this for fragment??

Any help would be helpful

Thanks.

Naucratis answered 30/3, 2016 at 16:40 Comment(0)
T
7

You certainly can do this. You'll need to parse the intent in the activity and use the fragment manager to populate late the fragment you wish. Replace Action and Fragment with your own.

@Override
protected void onNewIntent(final Intent intent) {
    super.onNewIntent(intent);
    parseIntent(intent);
}

private void parseIntent(Intent intent) {
    final String action = intent.getAction();

    if (action != null) {
        if (Action.<ONE>.equals(action)) {
            FragmentManager fm = getFragmentManager();
            Fragment<ONE> fragment = (Fragment<ONE>) Fragment.instantiate(this,
            Fragment<ONE>.class.getCanonicalName(),
            getIntent().getExtras());
            FragmentTransaction ft = fm.beginTransaction();
            ft.add(R.id.fragment_id, fragment);
            ft.commit();
        } else if (Action.<TWO>.equals(action)) {
            FragmentManager fm = getFragmentManager();
            Fragment<TWO> fragment = (Fragment<TWO>) Fragment.instantiate(this,
            Fragment<TWO>.class.getCanonicalName(),
            getIntent().getExtras());
            FragmentTransaction ft = fm.beginTransaction();
            ft.add(R.id.fragment_id, fragment);
            ft.commit();
        } 
    }
}

Actions are just strings that should be unique for a given intent. They can be anything. Like:

"myapp.image_included" or "myapp.link_url" etc

Thwart answered 30/3, 2016 at 16:50 Comment(3)
Do you have an example or link to one? I tried googling but couldn't find anything. THanks @Cory RoyNaucratis
In this case what is Action when calling Action.<ONE>.equals(action) please?Steersman
@MicheleLaFerla It's just a string comparison.Thwart
L
3

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")

Luanneluanni answered 19/8, 2021 at 7:58 Comment(1)
When using this approach, would you still need to set launchMode="singleTask" of the Fragment's Activity to avoid a new instance beeing created?Travelled
G
0

yes possible, you can call fragment inside activity(from which activity your going to fragment class) - call inside onCreate method

Intent appLinkIntent = getIntent();
String appLinkAction = appLinkIntent.getAction();
Uri appLinkData = appLinkIntent.getData();

String recipeId = appLinkData.getLastPathSegment();

if(recipeId.equals("mybag")){
  call fragment 1(i am sending link like this -(example) 
   www.google.com/mybag)
 }else{
 call fragment 2
}
Galliwasp answered 27/4, 2021 at 13:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.