Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 799
huey77 Avatar asked Mar 30 '16 16:03

huey77


People also ask

Can you deep link into an app?

Passing search data via deep linkingDevelopers will need to submit their app and deep linking apps on both iOS and Android to be indexed by Google. Alternatively, developers can use Google's short links to deep link mobile app users if the app is installed and direct others to the webpage.

How do I integrate a deep link app on Android?

First, you specify the BROWSABLE category so your deep link can work in a browser. Without it, clicking the deep link on a browser won't resolve to your app. Second, the DEFAULT category lets your app handle implicit intents. If it's missing, the intent must specify the app component name for the activity to start.

How does deeplink work on Android?

A Deep Link is a URL link that is generated, when anyone clicks on that link our app will be open with a specific activity or a screen. Using this URL we can send a message to our app with parameters. In WhatsApp, we can generate a deep link to send a message to a phone number with some message in it.

What is difference between app link and deep link?

When a user click an URL, it might open a dialog which asks the user to select one of multiple apps handling the given URL. On the other hand, An Android App Link is a deep link based on your website URL that has been verified to belong to your website. When user clicks that URL, it opens your app.


2 Answers

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

like image 98
Nicolás Carrasco-Stevenson Avatar answered Sep 18 '22 18:09

Nicolás Carrasco-Stevenson


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

like image 39
Cory Roy Avatar answered Sep 21 '22 18:09

Cory Roy