Issue
Following procedure:
- Start my application, Stack: [HomeActivity]
- Going to Facebook, using a deep link to get into Activity X
- Pressing back button results in getting back to HomeActivity instead of Facebook
Expected
- Start my application, Stack: [HomeActivity]
- Going to Facebook, using a deep link to get into Activity X
- 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.
Solution
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).
Answered By - David Wasser
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.