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

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.

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

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

Related

Navigation Component implicit deep link back press exits the app

I've an implicit deep link created just like mentioned in the docs.
https://developer.android.com/guide/navigation/navigation-deep-link#implicit
implicit - domain.com/
When I click on it, yhis opens a new instance of the activity, mentioned in the docs đź‘Ť
If I press back it exits the app.
The documentation says it should go back to the previous app and reloads that fragment, what am I doing wrong here?
If the flag is not set, you remain on the task stack of the previous app where the implicit deep link was triggered. In this case, the Back button takes you back to the previous app, while the Up button starts your app's task on the hierarchical parent destination within your navigation graph.
What's the difference between back button and up button?
The documentation says it should go back to the previous app and reloads that fragment, what am I doing wrong here?
The docs you've specifically quote says that the system back will take you back to the app that deep linked into your app, so the behavior you are seeing is expected.
For example, if you click a link in the Discord app and that app doesn't use FLAG_ACTIVITY_NEW_TASK, then your app exists on Discord's task stack and are part of its back stack. This means that the system back button is expected to take you back to Discord.
As per the Principles of Navigation, the Up button functions differently when your activity is placed on another app's task stack:
The Up button never exists your app
When your app is launched using a deep link on another app's task, Up transitions users back to your app’s task and through a simulated back stack and not to the app that triggered the deep link. The Back button, however, does take you back to the other app.
So it is expected that the Up button always keeps the user in your app and the Up button will never return the user to the Discord app.

Android Task and App Link - Check which is the previous Activity in task when app link opens an Activity

Scenario :
In my application i have enabled app linking. So when an App link is triggered, i have set a dummy AppLinkActivity (opens as "singleTask" to avoid creation of new task) to receive and handle the url received through the app link. While handling the url, i have to check whether the app was showing the "DownloadActivity", if so then i have to show a dialog.
So when an App link is triggered, i will have the already existing task with existing activities and on top of that will be the AppLinkingActivity. In this case i have no way to know what the previous activity was, since i cannot pass anything via an intent.
What i want to do
When the AppLinkingActivity is opened via an app link, is there any way by which i can find out what was the last shown activity(of my app) before AppLinkingActivity in the same task?
--Alternatively--
If i exclude the "singleTask" in manifest then the AppLinkingActivity is opened in a new task. In this scenario, is there any way by which i can get the last shown(or topmost) activity in the previous task? If yes, i also need a way to navigate to that topmost activity in the previous task? In this scenario if i simply finish() AppLinkingActivity, then the control goes back to the which ever source that triggered the app link like browser or gmail app etc.
Is any of the above two requirements is possible?

How to close app after opening browser intent

Scenario:
I have an app, let's call it app A.
A opens an activity of App B, using Intent.ACTION_VIEW,
and then calls finish().
Problem: App A is still found in recent apps even though I finished its last activity. However, it's window looks black.
Question: Is it possible to detect when the "Complete action using -app-" screen is finished so I can programmatically close my app or how can I properly make sure the app is gone after going to App B?
There are several things here:
The list of recents is NOT a list of recent apps, it is a list of recent tasks. If you launch another Activity, then that Activity (even if it is from a different App) usually ends up in the same task as the Activity that started it.
If you don't want your app to show up in the list of recent tasks, then you should set android:excludeFromRecents="true" on the root Activity in the manifest of your app.
You can also launch App B in another task, by adding Intent.FLAG_ACTIVITY_NEW_TASK to the Intent you use to start App B. This will cause App B to run in a different task than your app A.
I've no idea why, when you return to the task started by App A, the screen is black.

What's the difference between opening an app from the applications screen and the recently used apps list? (android

Can anyone tell me what the difference is between opening an app from the applications screen and opening it from that recently used apps list that pops up when you long-press the home button?
I didn't even know that recently used list existed until a friend managed to break my app by launching it from there. He tried twice and got the same force quit, but when he launched it from the applications screen it opened fine.
The error log told me that a nullPointerException occurred in the getCount method on my ArrayAdaptor for my ListView.
Anyway I just wondered if there was a difference that I need to know about and adapt my code to deal with?
AFAIK, If your application is completely shutted down, launch from applications screen and recently used apps list should have no difference, both refresh start your application and open your application's MainActivity (by stack-push your application's MainActivity into a newly created task)
However, as Android is multi-task OS, your application can be put into background in standby mode i.e. open your application then short-press home button, this is not same as press back button. If you haven't override these key pressed in your application, press back button several times with pop all your activities off from activity stack and finally kill your application, whereas press home button will bring System's HomeActivity into foreground hence flip your application (AKA. task with activity stack) into background.
Things becomes more interesting here, depend on which value your configure your activity's android:launchMode in AndroidManifest.xml, if you use standard or singleTop:
1. launch app from recently used apps list always bring your standby activity back to foreground, i.e. re-order activity stack.
2. launch app from applications screen will create a new instance of your MainActivity and open it, i.e. push a newly created MainActivity into activity stack, so now you have two instances in your application's activity stack
If you use singleTask or singleInstance:
2. launch app from applications screen will use the standby MainActivity (if exist) in your application's activity stack and re-open it, i.e. re-order activity stack.
Checkout Tasks and Back Stack to see how different configurations may affect your application's activity stack behaviour.
I believe there should be no difference. These are the lifecycle methods I typically see when pressing the home button from an activity, on android 2.3.4
onPause
onStop
then when I use either the icon or previous applications to navigate back, I see
onRestart
onStart
onResume
Now, in some cases the system will tell your activity to finish while you are away (or immediately when you return if an orientation change occurred). Then you will see onDestroy, and the following when you navigate back
onCreate
onStart
onResume
I don't think there is anything mysterious going on here. According to the Activity documentation, there are only four states that a process can be in, and both of these fall under background activity.
There shouldn't be any difference in how the activity is launched from history, apart from the fact that the launching Intent will have the FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY set.
Here's an easy way to think about it. All of your activities are launched form Intents. Holding down the home button allows you to open that activity using the last intent that launched it. This can give you some unexpected results however. For instance, if you are able to launch your activity from something special like a widget.

Activities of one App seem to be in own process

I have two activities, but they are in different packages.
Now I call an Intent in the first activity to launch the second activity. This is working. Also I can get back with finish().
But now I kill the the app with advanced android task killer. The app disapears in the list
If I was on the second activity and then go to advanced task killer to kill the app and after that I start the App again, it launches the App on the second activity.
I have never seens such a behavior.
Also if I fire an Intent on the second screen in order to get back to the first activity and then call there System.exit() the App jumps on the second screen.

Categories

Resources