Mahoni
Mahoni

Reputation: 7466

Back button takes me to launcher activity not deep-linker activity

Following procedure:

  1. Start my application, Stack: [HomeActivity]
  2. Going to Facebook, using a deep link to get into Activity X
  3. Pressing back button results in getting back to HomeActivity instead of Facebook

Expected

  1. Start my application, Stack: [HomeActivity]
  2. Going to Facebook, using a deep link to get into Activity X
  3. Pressing back button results in getting back to Facebook App

I get the expected behavior when my application is not started at all beforehand. I see that other apps like Instagram does managed to get this working properly. So even if your application is running in the background it takes you back to the activity which issued the deep-link intent.

My activity has launchMode="singleTop", onBackPressed() is not overriden, so it calls the super class implementation.

What am I missing here to get this right?

I debugged it and onBackPressed() eventually calls finish(), yet it gets me back to my application instead of Facebook.

Upvotes: 3

Views: 1536

Answers (2)

David Wasser
David Wasser

Reputation: 95626

Add

android:taskAffinity=""

to the <activity> tag for your "deep-linked Activity" in the manifest.

What is happening is that Facebook is launching your "deep linked Activity" with Intent.FLAG_ACTIVITY_NEW_TASK (you should be able to verify this by checking the content of the Intent in your Activity in onCreate() or onNewIntent().

If your app is already running, Android brings your existing task to the foreground and launches the "deep-linked Activity" on top of that task. When you then press BACK, it just finishes your "deep linked Activity" and drops you into your existing task.

Android does this because all of your activities share the same taskAffinity, so when it needs to create a new task for your app, it will first try to find an existing task with the same affinity.

If you set the taskAffinity of your "deep linked Activity" so that it is empty, this should prevent Android from looking for an existing task to launch the Activity into. It will just create a new task and launch your "deep linked Activity" into that new task. Then, when you press BACK, your Activity is finished, and the task will become empty, so the task will be finished and it will drop you back into the previous task in the task stack (which should be Facebook, since your app was launched from there).

Upvotes: 4

Mark Renouf
Mark Renouf

Reputation: 31020

The reason is that the launch of Facebook starts a new task. Back always navigates up the activity stack within a task.

If you have control over the intent which launches Facebook, there are flags which control the task the activity is launched within. The default is to launch within the same task.

I suspect that Intent.FLAG_ACTIVITY_NEW_TASK is being added intentionally by the system -- so this may be by design (working as intended).

PS: This presentation will teach you all you ever need to know about android activities and tasks: http://www.slideshare.net/RanNachmany/manipulating-android-tasks-and-back-stack

Upvotes: 0

Related Questions