Gligor
Gligor

Reputation: 2940

Android Activity back stack and multitasking support

I have an app that supports multitasking (working in the background), however I have run into problems with the android backstack.

This is what I have:

Activity A starts Activity B for result, so...

Activity A --> Activity B

If when at Activity B the user long presses the home button and switches to another application (say the browser for example) and then long presses the home button again and comes back to my app, they will be at Activity B, however the back stack at this time will look like this:

Activity A --> Internet Browser --> Activity B

So when I do finish() to send back a result from my Activity B it does not come back to my Activity A, but rather to the Internet Browser...

This is also the case if the user doesn't use long press of the home button, but also uses the home button to come back to their launcher and then uses long press home button to come back to my app. In this case the back stack is even worse:

Home Launcher --> Activity B

So when I do finish() on Activity B, the user gets back to their home screen and they can never get back to Activity A except for if they go and start the app again from their app drawer.

Is there any way to implement multitasking work in this case? Activity B needs to always return back a result to Activity A no matter what the user opened in-between these two.

Upvotes: 3

Views: 1582

Answers (1)

Gligor
Gligor

Reputation: 2940

OK. After long hours of research and trying various things, here's the solution to the problem. Hopefully this helps others...

The solution is pretty straight forward and simple, in AndroidManifest.xml

set android:launchMode="singleTask" for Activity A

set android:noHistory="true" for Activity B

This way Activity B will be removed from the Stack if we go to another app like the browser or exit to the home screen, so when we come back to our app we get back to Activity A.

Upvotes: 2

Related Questions