Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android deep linking - Back stack

I am trying to implement deep linking in my Android application. I have been following this guide. I have an Android Activity that is started from and intent-filter in the Android manifest:

<activity
    android:name=".MyActivity"
    android:parentActivityName=".MainActivity" >
    <intent-filter android:label="@string/filter_title_deep_link">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="com.example" />
    </intent-filter>
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".MainActivity"/>
</activity>

I am spawning this intent from adb:

adb shell am start -W -a android.intent.action.VIEW -d "com.example://test" com.example

The activity is being created with the correct intent data and runs as expected. However, on press of the back button, the application exits. I was expecting the back stack to be built with MainActivity, as specified by parentActivityName in the Android manifest. Obviously this is not the case.

How can I add a parent activity to the back stack in this case?

I wondered if I could use a TaskStackBuilder as shown here in the context of notifications, but wasn't sure how it would work.

Perhaps I should have an intermediate Activity to build the main activity using something like:

TaskStackBuilder.create(this)
                .addParentStack(MyActivity.class)
                .addNextIntent(new Intent(this, MyActivity.class))
                .startActivities();

?

like image 516
Jon G Avatar asked Jun 17 '14 17:06

Jon G


People also ask

What is Android deep linking?

In Android, a deep link is a link that takes you directly to a specific destination within an app. The Navigation component lets you create two different types of deep links: explicit and implicit.

What is back stack in Android?

A task is a collection of activities that users interact with when trying to do something in your app. These activities are arranged in a stack—the back stack—in the order in which each activity is opened. For example, an email app might have one activity to show a list of new messages.

How do I find deep links on Android?

Test your deep links You can use the Android Debug Bridge with the activity manager (am) tool to test that the intent filter URIs you specified for deep linking resolve to the correct app activity. You can run the adb command against a device or an emulator.

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.


Video Answer


3 Answers

I came across the exact same problem. So, if you want your user to go to your parent activity, whenever they presses the UP button, you can define the parent activity in the AndroidManifest.xml and then programmatically control the up-navigation.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // Respond to the action bar's Up/Home button
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onBackPressed() {
    NavUtils.navigateUpFromSameTask(this);
}

You may do the same in all activities to constantly navigate the user up back to the home screen. Additionally, you may create the full back stack before navigating the user back. Read more in the following documentation.

Providing Up Navigation

A Straight Forward Solution

You can simply check if the deep-linked activity has a back stack to go back in your app's task itself by calling isTaskRoot(). I'm not quite sure if it does have any caveats though.

@Override
public void onBackPressed() {
    if(isTaskRoot()) {
        Intent parentIntent = new Intent(this, ParentActivity.class);
        startActivity(parentIntent);
        finish();
    } else {
        super.onBackPressed();
    }
}

In this case, you don't really have to declare parent activities in the Android Manifest.

like image 111
C-- Avatar answered Oct 17 '22 03:10

C--


Have you tried doing this,

Intent intent = new Intent(this, MyActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(intent);
stackBuilder.startActivities();

You have to build your own App stack in case of deep links.

like image 22
user3773337 Avatar answered Oct 17 '22 02:10

user3773337


i have been Working for App links and App indexing feature of Android with the basic of Deep Linking, I hope this is useful to index app pages and allow google To crawl the app as specified here Deep link Guide

  • The Major rule i studied in Deep linking and App indexing is to give First Click Free Experience to the User who launches from search or somewhere.and shouldn't contain any Login/Signup Page. However onBack button press event this must Go back to search results or originated place not to your Parent Activity. Source App indexing Best practices and Important

And this best practice applies for App indexing API,since you have referred the deeplink link from App indexing Training site from Android Developer site.

like image 28
Shivasurya Avatar answered Oct 17 '22 03:10

Shivasurya