Android Activity back stack and multitasking support - android

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.

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.

Related

Android activity restarts wrong Activity after system "kills"app

I have an app with several activities. I enter my app, go from activity A > B > C. Go to home screen. I click the launcher icon, it resumes activity C. All good so far. However, if I repeat this, but then open 10/15 apps, and try it again, Android restarts activity A with a backstack of C, B, A. Clicking back should go home, but it resumes activity C.
I've tried singleTop and standard launch modes for the main activity. No help!

Clear back navigation but keep activity alive

I have an app that starts with Activity A. Among other things user can go to Activity B and return with the back button.
User can start Activity A also from Activity B using a Menu. This brings Activity A to Front again.
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
Problem is: Activity A should be Home Activity. Pressing back needs to always quit the application.
Currently back will go back to Activity B, then finish.
Using FLAG_ACTIVITY_CLEAR_TOP will destroy Activity B, which I do not want as the user should find the activity where he left it (e.g. scrolling position; unless android destroyed it for memory reasons).
I just want to leave the application on back pressed on Activity A. How can I do that?
Can I clear the backstack without destroying the activities?
As an alternative I could override onBackPressed in Activity A to quit the app, but how do I exit the application? Using finish() will only close current activty an bring back other open activities. Do I nees a broadcast as suggested here: Clear Activity back stack?

Android back button and resume activity

I have this issue.
I have an actvity A that starts other activity B (by onclick - button).
In B I have one back button to come back to activity A. I press it.
Now that I'm in A, I press again button to go to B.
If I use android back button (I'm in B) I come back to A first and then to B.
But now, if i press android back button again, I don't go to previus activity of A or it exit from app. I come back to B !!!
How can I prevent this behavior ?
The back button of activity B should not start activity A, but close activity B with finish:
http://developer.android.com/reference/android/app/Activity.html#finish%28%29
Note that it is probably bad user interface design to have a "back" button on the interface. This official Android page says "Don't use labeled back buttons"
http://developer.android.com/design/patterns/pure-android.html
if you wish your custom back button to swap between the acivities A and B, you should simply launch intents, that will do.
I believe your problem is with the android back button. If you do not want go to the previous activity with this action, you can write in the manifest file under the <activity> tags for both A and B activities - android:noHistory="true". Doing this will exit from the app. as the activity stack was storing none of the activities, but such a requirement is quite confusing UI approach.

Android : Activity Stacking Issue or System Launching Issue

The main/launcher activity in my app is a login page (Activity A). Once the user is authenticated, they are taken to the main area of the application, e.g. Activity B. So now the current activity stack of this task is A > B.
I then press the home button on the phone and am taken to the Android home screen. I re-launch my app via short cut key in HTC Desire Z(See Image after Space there are two Short Cuts 1 and 2), and I am taken to Activity A, instead of Activity B. Either the activity stack is now A > B > A, or there are now two separate tasks with activity stacks A > B, and A respectively. What I want is to be taken back to Activity B when I relaunch the app..
I followed this link
The above solution worked for 2.3.3 but in ICS 4.0.3 it has an issue that i am not taken to Activity B.
How do i resolve this,In ICS I am not able to see the what Intent flag system is using to launch activity when short Cut is Pressed,is this a System BUG?
Please Help
NITZ
The pattern that I tend to use for the login like this. I'll use A to mean Login and B to mean Main application.
I make B the launcher Activity, and in its onCreate() I check if a login is needed and if so, then I immediately launch Activity A. Once A is done, i finish() it, so that I'm back to B.
This way my activity stack stack never contains the Login activity, except when it's being used. ie, after a Login is done, then only B is on the stack.

Android, difference between LongPress Home and Home

In my app, I've noticed a difference when using the home button.
Ex.
I press app icon, and the app starts op like it should with Activity(A). When I press Home button, the app gets minimized as it should. When I press app icon again, the app resumes with onResume() with Activity(A)
Now for the tricky part.
From the app, when I press my "Start button" another Activity(B) is launched, and focus is changed to the new Activity(B). When I press Home, the app minimizes (like its supposed to).
And now for the problem:
If I press the app icon, my app starts op from the very beginning with Activity(A) where my start button is. (And not with ActivityB which I just minimized)
If I do the scenario over again (Start app - > focus to Activity(B) -> Press home) and this time long press the home button, and the "Recently launched" comes op, and I select my app. Now the focus resumes to Activity(B)
Both Activity(A) and Activity(B) are set to SingleTop.
My question is, why is there a difference when using the different combinations of LongPress home + icon and OneClick Home + icon
First think about this... when you are opening your application ...
your first Screen will be shown may be it is SplashScreen.
and if..you are in Activity A and goes to B and then Press BACK obviously
you will see Activity A.
So now when you Long Press Home button..That shows you recent apps with saves state of last shown Activity..that's why when you open from there..you see Activity B directly without SplashScreen.
And Simple Pressing Home Button Once,you are went to Home Screen from where you are opening Applications Screen which is Launcher Screen..from there If you press any Icon that particular app will be Launched(Means to open the app from its Main Activity that is defined in Manifest with Intent Filter set to Launcher)...That's why opening an app from there will always show you,your SplashScreen.
This is not a Bug,not an Issue..This is how the Android is designed to work.
Same button is used to perform two different events,cause they are related..but It's not like both event performs the same actions.
Continuing to the above scenario, I have an App with Login Screen which takes me to Activity A -> OnClick of some button takes me to Activity B.
when there is a LongPress of Homebutton,and I start another App and stay on it for considerable time like say 5-10 minutes. Then again When i Long Press home button, and return to my Activity - It starts with Activity B .. which is correct.
From Activity B if i press Back key, It does take me to Activity A but there is a Black screen shown. In normal cases, It shows me a a list view of Dynamic XML data.

Categories

Resources