I am trying to figure out how recent apps interacts with android back button event.
When my app is not opened in the background, if I launched an deeplink and it goes to my simply destination activity. When I clicked on back button, I can see that the activity popped from stack and my app closed due to the activity was the only one in task stack.
my questions,
Even with back button clicked, and the app closed. I still see my app in "recent apps", my impression is that such back button should killed my app since no activity in stack at all. Why still in recent apps?
If I click on the app in recent apps, I observed the same intent was triggered ago (deeplink intent), and why is that? (android's mechanism to cache last intent maybe?)
Related
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.
I have 4 Activities: Launcher, MainActivity(SingleTask), NotificationActivity, ExampleActivity.
When the user clicks on the notification, NotificationActivity opens then ExampleActivity and finishes the NotificationActivity.
When I press back MainActivity opens. Now if I press back, the application gets closed. If the user then opens the app from the recent Tasks, NotificationActivity opens then ExampleActivivty. But if the user opens the app from the icon, Launcher Activity opens then MainActivity.
How to handle this situation?
Exactly this problem is covered in my answer to this question:
Remove data from notification intent
When you select your app from the list of recent tasks, Android remembers how the app was originally launched (in your case, via Notification) and starts the app again the same way. This can be considered a bug or a a feature, depending on what you expect to happen.
I've got a few suggestions in my answer that you can use to deal with it.
I'm working on a launcher app that basically launches other installed apps on the device with an explicit intent and I have a edge case scenario:
An Activity (Act) creates an intent of an application (App) and starts it by calling startActivity(intent).
App get launched, my Activity going to "stop" state.
After a while I want to get back to my application so I click on "back" hard button that closes App and bring my Application to foreground (resume state).
This is the wanted behaviour.
Here is the edge case:
If I click on the "recent applications" hard button (square icon) while on App is launched, history stack is lost, and when I return to App, and click on "back" hard button - App exists to the Launcher screen and onResume of my application is being called.
I searched the web for a solution for couple of hours now, maybe I'll find a solution here.
Thanks
It seems to me you should set android:alwaysRetainTaskState true in your root activity.
What I do know is that the current activity get into the onPause() mode, and the home screen activity brought to front.
My confusion starts with situation you can re-open the application from the recent tasks menu.
so what exactly happening when I'm opening the application from the recent tasks manager?
Is the activity that was foreground when the home button pressed is still somewhere in the stack?
Is there more then one activity stack on the same time?
As far as I understand it, there is no real stack (of applications). Is just that your activity has states, so whenever you are pressing the HOME button your activity in your current application just "pauses" like if it was a stand-by state so multiple applications can be in this state as long as the system doesnt require memory and kill the tasks.
So whenever you open the activity from the recent tasks its just telling the application to wake up.
First of all, here is everything you need to know about the concept of the "Up Button":
Navigation with Back and Up and some of the implementation details: Providing Ancestral and Temporal Navigation.
Generally speaking, the Up button lets you navigate up in the application hierarchy, instead of just navigating back in the application(s) back-stack.
For example, if you work with some kind of app and you get the email notification, you can open the mail client by pressing the notification. Then you can go back to you application you were working with by pressing the Back button ( back-stack ) or you can press the Up button in order to go to the mail client's 'parent' activity ( for example from some EmailMessageActivity to EmailHomeActivity ) to work with the mail client application instead of the initial application ( the back-stack usually is cleared then, so you can only go back with the Back button as far as the the Android Home screen ).
The "Recent Tasks" factor is irrelevant and misleading, it's just another way of starting a new Activity.
My app is designed to run as a single instance and the Back button does not allow you to exit the app and return to the Start screen because it is used internally to navigate a hierarchy of screens where each screen can be an activity.
However, an external app can launch one of the app's internal activities. When the user is done with whatever the activity is designed for, the user's intuitive action is to hit the Back button to return back to the calling client. But because I prevent the Back button from exiting, the user cannot return.
I can add code to override this when the code detects that the activity is being launched by a client. The problem however is that if the app closes to return, the user might return to the app from where they left off. But since I closed the app to return to the calling client, the user cannot return back to the app as it was last opened. My app needs to remain as a single instance, so the activity that gets launched cannot be created more than once. Any suggestions on how to return back to the calling client but also keep the app running if it was running when the calling client used one of its activities?
In general, you can programmatically control the back navigation trail. Take a look at
TaskStackBuilder and the documentation for handling notifications Responding to Notifications. It seems that what you're trying to do is control the so-called "back stack". If you use TaskStackBuilder, the behavior will match the platform version you're on.
In pre 3.0 platforms, the Back button went all the way through the back stack to the first task the user did since the phone was turned on. Post 3.0, back does not traverse task boundaries; to get to other tasks, the user clicks the "Recent Items" icon. There's also the "up" icon in an app to navigate to the "beginning" of a task within an app. TaskStackBuilder will "do the right thing" for all versions.
In the current platform version, not allowing Back to exit your app is OK, because Back should only go to the first Activity in the current task. In versions previous to 3.0, not allowing Back to exit the app is more problematic, and I personally wouldn't do it that way, but it's up to you.
What happens when the user clicks Back after an Intent starts your app should be clear from the documentation I've cited. Basically, what you want to do is go back to the previous task.