Resume browsing on Chrome Custom Tab - android

I am working on an special oauth flow on Android. I have an activity A which can delegate the authentication to a webpage using a CustomTabIntent launched by UI-less Activity B. Once the authentication finishes it redirects with a deep link that B intercepts and closes the activities clearing the backstack which looks like
A -> B -> CustomTab
So when it finishes B with FLAG_ACTIVITY_CLEAR_TOP it goes back to A
Now for a new feature the difference is that the deep link intercepted by B needs to open C to do some permission management. I am able to do that by just finishing B and launching C. However once C finishes I need to take my user back to the browser to finish the authentication so I need a backstack that looks like
A -> B -> CustomTab ->C
But I seem unable to launch C on top of CustomTab without closing the later. Closing it is not an option since I need my user to continue at the point where he left the browser. Another option would be if I could close the Custom Tab but later resume the browser at the state it was closed. But I haven't been able to find a way to do neither of these.
Now to complicate the problem more, C could be a new instance of A ending with something like
A -> B -> CustomTab ->A2
Therefore launching my activities as single tasks is not an option.
If someone has been able to launch an activity on top of a custom tab without closing it I would really appreciate your help. Or if you have any other idea to tackle this problem it would be welcome.

Related

Move an app back on front without changing it?

I have an app that acts as sortofa bridge between two other apps, A and B, both of which are mostly black boxes. The user almost always stays in app A but sometimes needs to do something that my app or app B know how to handle. App A supports external actions via URL so I have a custom scheme://host defined for my app.
If the user does an action that my app handles, I get launched via the URL then do my thing then call finish() and the user's back in app A where they started (edit: same activity and etc) - perfect.
If the user does an action that app B handles, my app needs to be in the middle so it can do some translation. App A launches me, I format the message for app B and launch it. When it's done and comes back to my app, I call finish() and.. end up back in app B. Understandable I guess but I really want to end up in app A.
So the question is, how to I tell Android to put A back on top/in front without changing it? I can find the URL for app A via getReferrer() but when I put that into an Intent, it acts like I'm restarting app A instead of just going back to where it was. The doc for Intent.FLAG_ACTIVITY_NEW_TASK looked promising but it still acts like a new instance of the app.
It seems like this should totally be possible but I'm not seeing it. Java/api25.
The problem is that App B is not ending, returning control to your app. App B is launching your app, putting your app on top of it. When your app finishes, your app goes away revealing the still running App B underneath.
So basically App B has bad behaviour (at least, behaviour that is inappropriate for what you are trying to do).
To solve the problem you can either fix App B, or you can do the following:
Instead of just calling finish(), when you want to return to App A just launch it the same way that Android launches an app when the user presses on the app icon:
Intent intent = getPackageManager.getLaunchIntentForPackage("package.name.of.app.A");
startActivity(intent);
finish();
This will bring App A to the front in whatever state it was in when it was put in the background (ie: doesn't actually launch any new Activity). If App A isn't running, it will launch the root Activity of App A, as if the user pressed the app's icon on the HOME screen.
I'm assuming that both App A and App B are running in separate tasks. If that isn't the case, I'll need you to provide more information about which activities are running in which tasks.

Android deep link to resume app

Is there a way for a deep link into my app to simply resume the app if it's already running and preserve the activity stack?
Lets say I have activities A, B. Both are singleTop.
A is my main activity and it is also the one that has the intent filter for the deep link.
If I open A, and B on top. Then pause the app to go to a browser to deep link into my app. It opens just A where I want it to have B on top of A.
Is this possible?
Maybe your deep link takes the app to a dummy activity C (and that is the only way to start that activity, for simplicity) and from there you find your way back to the previous activity (either A or B).

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.

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

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

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