Navigation Component implicit deep link back press exits the app - android

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.

Related

deep linking - handle onBackPressed in situation where was already launched

i want to implement deep-linking as requested here:
https://developers.google.com/app-indexing/android/test
"The back button returns to the previous screen.
After opening a deep link, pressing 'Back' from the deep linked content should lead users directly back to the search results page. Test this by creating an HTML page with deep links (described below). After following one of the deep links from the browser to the app content, the 'Back' button should take the user back to the page containing the deep link. It should not lead to other content within the app or prompt for confirmation."
my problem is when my app is launched - > first activity is Started-> pressing home button -> using deeplinking (and now i am starting a different activity) -> back button is not getting me back to search results page. instead, onResume() called on the first activity. System.exit(0) not helping as it makes the app relaunch again (when onBackpressed is called).
thanks
It's depend on what you want to do.
1. Start a new application for deeplink
2. Start new application if app is already not running and if running us that app and go to appropriate activity.
in 1st case it's possible to come back to page from where deeplink was launched but I am not sure about 2nd case when app is already running.

Android detect activity resume via HOME or BACK keys

I have made my application HOME application, so that when you press HOME button, you are redirected to my application. From my application you can open other applications like browser and then return to my application either by pressing BACK or HOME keys.
The problem now is that I need to know when user returns using HOME and when using BACK key.
I tried printing Intent information, but it appears to be the same in both scenarios.
EDIT I was checking intent in onResume using getIntent(). After overriding onNewIntent method I now get a different intent there when user returns using HOME button, but this method is not called when BACK is pressed. Is it safe to assume, that if onNewIntent with android.intent.category.HOME is called, then user returned using HOME button and otherwise returned using BACK button?
onNewIntent() is fired when an app is running, and receives another intent to be launched. That is why you are seeing it when HOME is pressed.
When BACK is pressed, your app is not going to receive an intent. All that happens is that the apps on top of yours are removed. So your app appears from the back stack with only onResume() being called.
So that is how you can tell.
For interest, you can look at the source code for the older launchers online, and you will see that onNewIntent() is used to re-centre the launcher view on the main page i.e. it can help you to see if the HOME key has been pressed twice.
I was exploring this exact topic a few months ago (end 2013), and looking at production launcher code really helped.

what exactly android's home button doing?

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.

Returning to calling client with Back button when normally Back is disabled

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.

Android - nuke the previous app history

Here's scenario. Say I have an app that I was using and navigated from activity to activity so now there's some history there. Then I switched to another app so my first one is on the background. If I return to it I would be able to click "Back" to navigate the history and traverse 1st app steps.
Now imagine that I also have a notification that when clicked will bring one of the activities of the first app. Currently when that happens and I hit "Back" I will return to the previous history stack. My requirement - if I call activity using notification then I should have no history, If I hit back, the app should simply exit. Is is doable and how?
This is related to my other former question which in the retrospect didn't really got the answer
Try looking up the various launch modes such as FLAG_ACTIVITY_CLEAR_TOP

Categories

Resources