Clear the back stack of 2 different apps in android - android

I am integrating my React-Native app (app A) with another app which is in Java (app B). In order to do so, when the app A opens, it will open a deep link to app B.
import {Linking} from "react-native";
if(importStatus !== "done"){
Linking.openURL("some-domain://some-url")
}
Then in the app B, it will call the app A's intent and app A will be opened. This process is fine so far as I wanted. My problem is about the OS back stack. Currently in app A, when go back, instead of closing the app A, it goes to the app B and app B opens the app A again.
I would like to know if there is anyway to reset this back stack so when I hit the phone's back button in the app A, it closes the app A. (And the app B to be removed from the back stack)

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.

Back button clicked with empty stack but still shown in Recent apps

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

Launcher activity reopens when it is put into background and restored to foreground by clicking launcher right after installation process

An issue is about that a launcher activity reopens when it is put into background and restored to foreground by clicking launcher. This problem occurs only after first run when a user installs an app and launch it from a play store or an android package installer.
Here are steps to reproduce the issue:
The user installs the app from the play store or android package installer;
After installation the user opens the new installed app by clicking “OPEN” inside the play store app or the android package installer;
Inside the app the user goes to another activity and put the whole app into background by pressing a home button;
The user restored the app from background by clicking the app icon and new launcher activity is created.
In a result there are 3 activities instead of 2. Back stack looks like this:
Launcher activity;
Some other activity which was started by user inside the app;
Launcher activity.
Next, when the app is removed from a recent apps` list and above steps are performed again then the app behaves exactly as expected. New back stack looks like this:
Launcher activity;
Some other activity which was started by user inside the app.
I created example app to demonstrate the issue:
source code
Here is shown installation process and a first run from the android package installer:
first run
The user starts a SecondActivity inside of the app and puts app into background. Next, the user want to restore the app back to foreground and it turns out that a FirstActivity was created again.
Here is a back stack after above steps:
back stack after first run
Now, lets run the app in standard way:
normal run
Before this test the app was removed from recent apps` list. The user starts the SecondActivity inside of the app and puts it into background by pressing home button. Next, the user restores the app back to foreground and this time the user sees the SecondActivity. The app behaves as expected now.
Here is the back stack after above steps:
back stack after normal run
The issue doesn’t refer only to my example app. It can be verified using any app from the play store. I did such test on a Netflix app which was installed from the play store (btw. the Netflix home screen isn’t visible due to security policy):
netflix first run
Here are steps:
Install the Netflix app;
Open it from the play store;
Click “HELP” button on a main screen (inside the Netflix app);
Put app into background;
Click app icon;
The user sees again the main screen instead of a help screen. The back stack looks like this:
Main screen;
Help screen;
Main screen.
It can be verified by pressing back button then the user will see each screen one by one. As we can see the Netflix app behaves identical as my example app.
Can someone explain to my why does it happen?

Android - Listen for a specific application to launch and launch some other application instead

I am trying to make an application listen for a specific application (A1) to launch and then launch some other application (A2) instead (user's choice).
Currently I use two permissions, PACKAGE_USAGE_STATS and BIND_ACCESSIBILITY_SERVICE. The first permission is currently used to detect when the specific application (A1) launches. The second permission is used to simulate a button press, such as home or back.
I tried earlier to launch the other application (A2) as soon as the specific application (A1) launched using the code below with no success (application A1 is still on top).
Intent intent = activity.getPackageManager().getLaunchIntentForPackage(SPECIFIC_APPLICATION_PKG);
activity.startActivity(intent);
I realised that when leaving the specific application (A1) and going to the launcher/home screen then it was possible to launch the other application (A2). I simulated a back press as soon as the specific application (A1) launched and then launched the other application (A2) with success. The problem with this method was that when being inside a third application (A3) and performing the steps above I would end up in the third application (A3), in other words not launching application A2. To try and find a solution for this I thought of the idea to change the back press to a home press instead, this always takes me to the launcher/home screen from all applications. However if I originate from a third application (A3) and launch A1 (through for example the recents menu) then press home (through code) and try to launch A2 (through code), instead we go to A3 for some reason that I can't figure out.
All help is appreciated!
I found the solution in a previous asked question here on SO.
Starting an activity from a service after HOME button pressed without the 5 seconds delay
See Chinibin's answer

Is there a way to always launch the launcher activity after app was killed by system?

When I test my app on Android 8.1.0, I found a system behavior:
1. Open my app. Follow this path to open Activity B: LauncherActivity -> Activity A -> Activity B.
2. Press Home button, and go to app's permission page.
3. Revoke a permission.
4. Back to app through task list, and app crashes.
Actually, in Step 3, as soon as user revokes a permission, Android system kills the app process immediately. So when user reopen the app, the system will launch the top activity in the stack. In this case, it is Activity B. Activity B will call its onCreate(savedInstanceState) method with a non-null bundle, which is saved before the app process was killed. The other activities in the stack(LauncherActivity, Activity A) will not be instantiated, unless Activity B finish itself without starting other new activity, e.g. user just presses Back button.
Here is the problem, many foundational modules are initialized in Application and LauncherActivity. But in the above situation, when user reopen app, Android system will relaunch Activity B directly without launching LauncherActivity. This will cause Activity B crashes.
I have to initialize some foundational modules in LauncherActivity, not Application. So, Is there a way to always launch the launcher activity after app was killed by system?

Categories

Resources