Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook deep linking on Android

Tags:

I'm trying to implement Facebook's Deep Linking feature on my app and encountered the following scenario:

I have an activity called MainActivity which is declared like so:

    <activity
        android:name="com.mypackage.android.MainActivity">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

This activity + my package name are also declared in my app's settings on facebook developer website.

Once a link gets clicked on Facebook's app, I'm supposed to handle this event via the onCreate method of my activity. The following code handle the event:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Uri target = getIntent().getData();

        if (target != null){
          // got here via Facebook deep link
          // once I'm done parsing the URI and deciding
          // which part of my app I should point the client to
          // I fire an intent for a new activity and
          // call finish() the current activity (MainActivity)
        }else{
          // activity was created in a normal fashion
        }
    }

All goes according to plan except for the following scenario:

  1. User launched my app
  2. MainActivity created
  3. SecondaryActivity created
  4. MainActivity finished
  5. App goes to background via the device home button
  6. Deep link gets clicked on Facebook's app

In this case my app goes to foreground again, but MainActivity's onCreate / onNewIntent don't get called, instead SecondaryActivity's onResume() gets called and restored to it's last state.

Note: I've tested this issue on a Samsung Nexus with Android 4.2.1 and got to this result, though when tested on Galaxy S1 with Android 2.3.5 it worked as I initially expected.

Any help would be greatly appreciated, Thank you.

like image 385
woot Avatar asked Apr 24 '13 15:04

woot


People also ask

How do I enable deep link on Android?

Android Studio makes it very easy to test deep links. Click Run > Edit Configurations to edit the configuration of the project. Open the General tab at the top and enter the URI in the Deep Link field in the Launch Options section.

What is deep linking in Android?

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.

How do I deep link my Facebook page?

To deep link to a specific page, use fb://page/{page_id} for Android and fb://profile/{page_id} for iOS.


2 Answers

Facebook is starting your app from their own app by explicitly start your "MainActivity" (the one your provided them in the developer page).

by that - Android's default behavior is: if the application already runs, then calling again to startActivity() won't start new task from scratch, but only restore to foreground the already running task.

but the good news are that you can change this default behavior by adding to your MainActivity the android:launchMode="singleTask". it definition is:

the system creates a new task and instantiates the activity at the root of the new task. However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent() method, rather than creating a new instance. Only one instance of the activity can exist at a time.

from this point you could always respond to the starting intent, and from that point you can always navigate back to the task that already was in background(if exists) by restarting activity with both flags Intent.FLAG_ACTIVITY_SINGLE_TOP && Intent.FLAG_ACTIVITY_CLEAR_TOP combination

like image 98
Tal Kanel Avatar answered Oct 11 '22 14:10

Tal Kanel


See http://developer.android.com/guide/topics/manifest/activity-element.html

You can play with:

android:clearTaskOnLaunch
android:noHistory
android:launchMode
like image 22
Giuseppe Avatar answered Oct 11 '22 13:10

Giuseppe