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.
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?
I have an app that interconnects with Google Drive through their Java Rest API to open documents.
I am using the following code to open the document on Google Drive
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(results.get(0)));
startActivity(browserIntent);
The thing is the user can return to my app with back button if Google Drive was previously closed, but if its already opened the user can't get back to my app with the back button and instead returns to previous activities inside Google Drive.
I tried setting several flags like the following to no avail
browserIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
browserIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Using those flags don't fix my problem what flags can i use then?
If you are opening another application then you can't control its Activities stack which makes sense.
So if App is already running then its stack might contain activities. Which comes to screen when user press back key.
Functionality which you want can be added, like we can check user came from another Application then kill my App(third party/launched App) on press of back. But what will happen according to the user perspective if he navigated through few options and put his App in background for later work then suddenly everything clear.
I am trying to create an app in which i am using background serviced plugin for uploading data to server.Then i tried to go back to the app home page when a background service will start so the user can do other work while data is uploaded. but the issue is as soon as I click on the device back button it gives an network not found error and background service stops and it navigate to another page. Then i search how to navigate to device main page using device back button using home plugin know after starting the background service when i click on the back button it opens the device home-screen and the backgroung process continues. Can anyone tell me how to make an background serviced continue and navigate to another page.
In iphone when we exit app and start app again by clicking launcher icon the app start from screen which was open last time before exiting app.
We can achieve same in android when we exit app using center or home button on android phone. In android app if app is exited by pressing home button and started again by clicking on launcher icon the app start from activity which was open last time before exiting app.
Is that functionality similar to iphone in terms of keeping app in memory longer? Will that work in android all time because I think android system will remove app from memory after sometime or will it remain in memory longer? Can I depend on this functionality in android and expect it to work all time.
I have a project in android which client has asked me to have iphone like functionality if we exit app and start app again it should open from screen which was opened last time before exiting. Now this is possible in android only if we use centre button. And also if user has exited using centre button and started it again, app should check for user current location and do some other operations. If app is launched and user navigate to any activity is there any way to check if app was exited using back button or centre button so that I can run code if app is exited using center button.
Thanks
We can achieve same in android when we exit app using center or home button on android phone.
The HOME button does not "exit app".
The HOME button brings the home screen to the foreground, just as the CAMERA button (where available) brings the camera application to the foreground, the CALL button (where available) brings the dialer to the foreground, tapping on a Notification may bring something else to the foreground (e.g., SMS client) based on the Notification, etc.
In android app if app is exited by pressing home button and started again by clicking on launcher icon the app start from activity which was open last time before exiting app.
No. If you tap on a launcher icon, and the app's process is still in memory, the existing app instance will be brought to the foreground, returning you to whatever activity you had been on. If you tap on a launcher icon, and the app's process had been terminated to free up RAM for other apps, you launch a fresh copy of the app and bring up whatever the ACTION_MAIN/CATEGORY_LAUNCHER activity was that the user tapped upon.
Will that work in android all time because I think android system will remove app from memory after sometime or will it remain in memory longer?
The length of time that a non-foreground app's process will be in memory is indeterminate and will be based on what is going on with the device, plus the device capabilities (e.g., how much RAM). I suggest you read more about the process lifecycle.
Now this is possible in android only if we use centre button.
It is not possible "if we use centre button" (what Android developers refer to as the HOME button). It may happen automatically, but if the app's process has been terminated, it will not happen automatically.
If app is launched and user navigate to any activity is there any way to check if app was exited using back button or centre button so that I can run code if app is exited using center button.
You should not care whether the "app was exited using back button or centre button".
You should care whether you have your data and how old that data is, refreshing it if it is stale.
Whether the user left your app via HOME, BACK, CAMERA, CALL, a Notification, the recent tasks list, an incoming phone call, by smashing their phone to bits with a rock and replacing it with an exact duplicate, or by any other means, should not matter to you.
To draw an analogy, think of a Web app. In a Web app, you care about whether you have a session cookie and whether that session is stale (e.g., to force a fresh login). Whether the page request came because the user clicked a link within the app, or clicked on a link from a third party site pointing to your app, or refreshed their page, or used a bookmark to get at another page in your app, or double-clicked on a desktop icon that brings up your app, or right-clicked on a link and opened a fresh tab, or anything else, should not matter to you.
Review the Android Activity Lifecycle at http://developer.android.com/reference/android/app/Activity.html. If you do nothing, you have no guarantee of your app starting at the same point it left off, however you have control here. You can, for example, overload the onPause() method to save your state to a file, and onResume() to restore it.