I have an android app which is experiencing a bug after updating to a new version from the google play store. This bug only occurs on certain devices. On some devices (like my Nexus4) I don't see the bug. So let me explain the problem on devices that don't work.
For simplicity, let's assume my app has 2 activities, ActivityA and ActivityB. Let's assume ActivityA is the launch activity. Let's also assume there is a button on ActivityA that creates a new Intent to ActivityB (while keeping ActivityA in the task stack, so clicking the "back" button returns to ActivityA).
On certain devices, I noticed that after updating to a new version from the google play store, the app seems to launch a new version of itself (or a new task stack, not sure how to determine exactly what's happening).
My intention is to return a user to where they left off after an update. So if they were on ActivityB and they click the app icon, I want them to return to ActivityB (this happens with no intervention on my Nexus4, but doesn't seem to occur on other devices).
To deal with this issue, I put logic in my launch activity to go the activity the user was last using. I accomplish this by storing a variable in SharedPreferences on each activity's onStart() method. In the launch activity's OnCreate() method, I check to see if this SharedPreference variable is set, and if so, check if it's set to something other then the launch activity. If it's different, I create a new Intent to the activity referenced in the SharedPreference, and start that activity.
The problem I'm experiencing is that (on certain devices) it appears to launch a separate task stack when I do this. Here's what I see:
User starts app (launches ActivityA)
User clicks button (launches ActivityB)
OnStart() of ActivityB stores "ActivityB" in the SharedPreferences
User updates app to newest version on google play store (clicks the google play store icon, selects "My Apps", and clicks "Update All")
User clicks app icon shortcut from home screen on phone
Launch activity detects SharedPreference was set to ActivityB, so it creates a new Intent for ActivityB (this is the first thing the user sees)
User clicks back button and sees ActivityA
User clicks back button and sees ActivityB **
User clicks back button and sees ActivityA
User clicks back button and sees android home screen (app is shut down)
** here is where I would expect to see the android home screen, but it appears another another version of the app, or another task stack is still running.
How can I make sure there are no other tasks running after updating to a new version from the google play store?
I was able to solve this by setting:
android:launchMode="singleTask"
on my activities in the manifest.
Related
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.
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.
I am working on a fitness app which has a home activity which launches a workouts activity which launches a specific workout activity. In the workout activity, one may start a workout. Thereafter, one might want to then press the Home button and launch a music player or perhaps the web browser. At some point, one would probably launch the app again to return to the already running workout, but that ends up launching a new instance of the app. When I set the launchMode on the home activity to singleTask, it simply goes back to the existing home activity when I tap the launcher icon. What I would like is for it to go back to the workout in progress, which is where you would depart the app.
Essentially, I'm looking for behavior identical to iOS where it would simply restore the app to its current state if you "relaunched" the app and it was still running.
It is supposed to work as you've described. In most cases, it actually does work like that. However, there is a long-standing nasty Android bug which causes the behaviour you've described. This happens when you launch the app for the first time from an IDE (like Eclipse) or by clicking the "open" button on the Installer screen. To see if this is what you're seeing, just do this:
Go to Settings->Applications, choose your app and click "Force close"
Launch your app, do something, press the HOME button
Launch your app again.
You should return to where you left off. If not, something else bad is going on. If that is the case, please post your manifest in your question, because the problem is likely in these.
Don't try to use special launchModes to fix this. This just creates more problems.
See this answer for more information about the nasty long-standing Android bug.
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.
I have three activities, lets call them Act1, Act2 and Act3. There is a login form on Act1 which (upon successful login) goes to Act2 which has two options (1. Go to Act3, Go to Act3 with some extra data). The user then goes to Act3.
Of course, when the user presses the "home" button the android device, the application is minimized and held in memory until android needs to use the memory (in which case the App is destroyed). However, when the user presses the "home" button and then opens the app up again quickly, the app is restored to the Activity that was in the foreground before it was minimized.
I want to be able to minimize the app, and then once re-opened go straight to Act1 to prompt the user to login again. I do not want the app to be able to resume in Act2 or Act3.
Unless your application is really security-sensitive, the default behavior should be better for the user: typing their login and password every single time they launch the app can be very annoying. Take for example the native GMail application: it doesn't require you to reauthenticate every time it opens.
Now, if your application really needs that behavior (say it's a credit card safe or something like that), then my first guess would be to handle Act3's onPause() and call finish() from there. Just be careful not to call finish() twice (see isFinishing()).
Also, since this is a breakage of the user's expectations, make it clear to the user that your app behaves like that for their security, not because it wants to be annoying.
When the user moves away from your activity (pressing the home button for example), the onPause() method is called first. You should be able to handle your logic there (for example, calling finish() on Act2 or Act3).
Edit: heh, yeah, what he said :D