Difficulty understanding back stack and tasks - android

I read about tasks and back stack (http://developer.android.com/guide/components/tasks-and-back-stack.html) but still having few confusions . I was just trying different things and stcuk on one case . So lets take a example :
We have two apps A1 and A2. A1 has one activity say A1_first(also the main activity) and A2 has two activities A2_first(main activity) and A2_second. A2_second is a singleTask activity. A1_frist calls A2_second on button press and A2_first also calls A2_second on button press.
If I launch A2 I can see the A2_first screen and after button press I go to A2_second(as expected) but suppose I first launch A1 and after button press A2_second , now press home button and again A2 icon from launcher , I reached to A2_second but I expected to reach A2_first.
I didn't understand what I am missing . Can someone explain
pressing A2
A1_first ----- > A2_second ----> home -----------------> A2_second (Why not A2_first ?? A2_first is main activity for A2).

If you launch an application from the HOME screen, it doesn't necessarily take you to the first activity of that application. If the application is already running, it just returns you to where you left off in the application. This is what you are seeing. In addition, you've made things more complicated by using "singleTask" launch mode. In general, you shouldn't be using "singleTask" or "singleInstance" launch modes. These are very special launch modes used mostly for creating HOME-screen replacements. In any case, if you need to use one of the special launch modes you need to make sure that you have a different application icon for the activities that use these launch modes. If you were had different application icons for A2_first and A2_second, then it would be more obvious what is going on.

If I understand you correctly you are not exiting the application, but just pressing the home button. If the application state is not changed it will come up back from cache with the same activity opened as you left it before pressing home.
Try How to finish() an Activity when Home button pressed for more details on how to finish app on Home button press

If you start (successfully) activity B from the activity A, and then press "back" , you will be back in the activity A. Independently on which applications do these activities belong to.
There is no standard "home" command in Android for returning, sorry. On my phone, for example, "home" will return to the start screen, with all activities put to the background. Obviously, you don't mean it.
I this is not enough, put here the code, containing activities calling and processing of returns. It is hard to say something not knowing, what exactly your call buttons and return processes do.
And before understanding tasks and backstacks, I would advise to understand activities starting/returning first.

When you press the home button, A2_second just goes into the background. It does not end (finish). So when you click A2 icon, the system will look for the last accessed activity from A2 (if any). Since A2_second is available and is in a background state - The system will simply call it back to the foreground.
This is how android establishes multi-tasking. The whole app (all the activities) are never loaded into memory at once. Instead, an application is broken into chunks of functionality (activities) that can be loaded as and when needed. So when you call an activity from another app (calling A2_second from A1_first), and then press home, This activity (A2_second) will go to the background. When you click the A2 icon, since the system knows that A2_second is in the background, It is brought to the foreground since the user has accessed this last and is probably the part that he/she is looking for.
If you press "back" however, A2_second will be finished. After this, If you click on the A2 icon, then A2_first will be launched.
This way, different parts (activities) from various apps can co-exist in memory and provide a seamless experience to the user while keeping the system fast and snappy.

Related

Is Home Activity instantiated multiple times?

The official Dev Guide of Tasks and Back Stack says, activities can be instantiated multiple times, and Home Activity is taken as an example
So I tried it out as the graph illustrates:
Launch Activity 2
Press Home button
Launch Activity 1
Press Back button (so I return to Home screen)
Press Back button again
But I did not go back to Activity 1. Thus, it seems that Home Activity has not been instantiated multiple times. Is it so? If so, how is it kept in a Back Stack?
EDIT: Sorry, I should've clarified earlier that I didn't write any codes to test it. All I've done is just launching applications on favorites tray. I'd better go to read the source code and search for the behavior of Home Activity.
Anyway, I don't think Home Activity is a good example here to illustrate multiple instances.
Your issue might be that you might have called finish() in your Activity2. Or, the OS clears up the 2nd Activity before you return back to it. The behavior you are trying to attain on your own has no guarantees. You can't force an Activity to keep running so that you can return back to it.

Android Back-Button-Cycle Issue

I'm ware of androids life cycle and that it's not needed to add a "exit" button in the application.
But still, this back button stuff isn't really working out well.
You maybe know this issue from the default SMS-App that android comes with: you open it when you get a new message and exit it using the menu button or something else.
After like 20 times doing this, you then decide to exit the app using the back button, what happens? you have to go back though 20 views. every time you press back you return to the "all messages (by sender)" listview and when you press back there again you return to the message opend 20-1 (message 19). again you press back and return to the listview and again you press back and end up at message 18. till, after40 times pressing back, you finally exit the messanger app.
same happens when for example you got a action bar with a "home" icon which opens the main screen of your app. the user picks a action and the new activity starts. than the user clicks the home button and returns to the main screen. when pressing the back button - no matter if you call finish() in the onButtonBack listener or not, you the user would expect the app to exit, but in fact the app returns to the previous activity which is wrong.
such cycleing may happen for various reasons, thats why - even thought i'm aware of the supposed to be lifecycle of android - i wan't to EXIT (& destroy) the app when pressing back within a defined activity.
calling finish() dosn't help. if there's a previous activity it will re-open it. calling system.exit(0) isn't nice to do.
so: whats the right way to prevent such back-press-cycles and/or exit a application (WITH destruction)?
for better illustration of what i want to achieve: consider A, B, C being activities. a arrow (-->) illustrations a new intent call from the activity leftside of the arrow, rightside of the arrow represents the activity that is called. ex.: A --> B means activity A starts activity B. now here's what i want:
1) A --> B --> C pressBack:--> B pressBack:-->A pressBack:--> Exit
2) A --> B pressBack: --> A pressBack: --> Exit
3) A --> B --> A pressBack:--> Exit
as you see, back works as always, BUT when in activity A it exits the application.
the behaviour i got now is 1) and 2) as above but
3) A --> B --> A pressBack:--> A pressBack: --> Exit
keep in mind, i've already overwritten the onBackPressed listener of activity A with a finish() call. even calling system.exit(0) dosnt work. however, even if it would: its not what i want, i want the REAL way to do it android style - i cant imagine system.exit(0) is best practise.
Well this is the default behavior.
If you have another approach, just implement it.
One approach to deal with this is to use the android:launchMode="singleInstance" for activities that can be launched in a singleton manner (only one activity can exist)
For example, if the SMS page in the SMS app was a singleTop, it would have needed only one back press to remove all the SMS pages. It is a matter of choice
Another more aggressive way would be to finish Activities when you start another activity. Of course, such decision would risk making the app less friendly (android users are not accustomed to this behavior). Nevertheless, if this is used only where it may be considered acceptable then it might be acceptable.
A very acceptable place to do this would be a login screen: Once login is successfull, you start another activity (probably designed for logged in users) and finish the login activity.
Enjoy Finally, in my personal opinion, you can add an Exit button. Users will find it nice.
Check my post: Adding an Exit button to Android Application

Android - need different activities to be launched conditionally when launched from home screen

All,
i tried different things and finally thought i post this question.
My application has 2 modes. one is with out registration and other, with registration.
in without registration mode, application has to behave just like normal android application. means, if user goes like A-> B-> C and at C, he clicks home button and relaunches the app, the app should come back to C, which is normal behavior.
in second case, if user is registred user and he is at A->B->C and relauches the app using home button, then it should not take him to C but it should take him to "log in screen"?
how to achieve this as we will not know that activity is being started from another activity/home screen?
i tried "single top" with onNewIntent() but looks like onnewintent() is not getting called when launched from homescreen. all ideas are welcome.
onNewIntent is not recalled because when you press home button application just goes to background try onResume

Confuse about launchMode

I have Activity A which calls B, at which point let's say the user presses the home button:
1) If the user long presses the home button and brings out the recently launch application, it will bring the user back to activity B.
2) If the user opens my application via the shortcut ( pressing the icon of the application), it will create another instance of Activity A.
I did not set any launchMode for my activity, so I guess it's launchMode="standard", the default behaviour I want is to go back activity B.
Why is point number 2 happening? I always thought that both of the actions will lead to the same result.
Every time it should call activity B unless Dalvik machine thinks that activity B is a background process and the process space might be required for other applications,
At that point your activity may be killed and it may launch afresh when you click the icon.
It's weird...I did a total uninstall and reinstall of the app.
Without declaring launchMode, it's default to "standard", and it works just as designed.

Launcher not returning to the last-used activity in my application

I'm using a custom Launcher application with Widgets that are used to launch (and provide status updates) for an application.
The application consists of a few Activities - let's call them A, B and C for simplicity.
A is the launched Activity. The user proceeds from A to B and then to C (never in any other order).
At any time the user can press the 'Home' button on the remote control and return to the launcher application.
If the user then presses the 'Back' button on the remote control they always return to the Activity they were last using (A, B, or C).
However, if they click on the widget (rather than pressing back) the Activity that is brought to the front seems inconsistent!
So, here is an example of what happens
From (custom) launcher, use widget to launch application
Activity A appears
User presses a button that launches Activity B
Activity B appears
User presses 'Home'
Launcher appears
From (custom) launcher, use widget to launch application
Activity A appears NOT B
Sometimes I get Activity B instead of Activity A - but I'm not sure under what circumstances. I want to get the Activity at the top of the stack to be displayed and never any other Activity (Activity B in the example above).
I've read Google's documentation about the Activity flags ("single-task", "single-instance", etc...) but nothing seemed to jump out as the solution to my problem.
In my case, Activities A, B, C only make sense when run in that order - and it never makes sense to see a previous activity unless you explicitly go back to it.
I'm not sure if the problem is the way the widget is launching the application or whether there is something I need to specify in my manifest or something else.
Any ideas?
Thanks,
Dan
Isn't that what's supposed to happen? Isn't your widget launching activity A? Would you expect it to launch activity B if you are asking it to launch activity A?
(Althought you say that you get B launched sometimes. Isn't this related to the app being forced out of the memory?)
I'm sorry, but this is not an answer, but rather some additional information related to that same question.
I have found a pattern for Activities when relaunched after home button is pressed.
See my question for all my research:
Android Activity Stack is not working as stated in the docs - last activity in task stack not shown
Hope this can be of help...
I have been reading that the suggested fix for this strange behavior is the use of a "Dispatcher" class as Main that will launch the activity based on the application state...or you can also keep track of all the activities opened that need to be restored...but this can become really cumbersome when having a complex UI application design.

Categories

Resources