0

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).

user1875798
  • 263
  • 3
  • 6
  • 16

4 Answers4

2

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.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • Agreed David , I should not use single task but I am not using this to create any app , I was trying to do deep dive. Still I have a doubt , as I read when we click icon whole task comes into foreground (and you see where you left) but in this case A2_first and A2_second should belong to different tasks as given in "http://developer.android.com/guide/components/tasks-and-back-stack.html" : " SingleTask " The system creates a new task and instantiates the activity at the root of the new task . so if I am pressing A2's icon task related to A2_first should come in foreground. – user1875798 Dec 17 '12 at 11:28
  • 1
    No. It is more complicated than that. There is also the notion of "taskAffinity". Since A2_first and A2_second have the same "taskAffinity", when you press the A2 icon, it will bring any task to the foreground that has this taskAffinity. If you really want to have separate tasks then you need to use separate icons and set `android:taskAffinity=""` for A2_second. – David Wasser Dec 17 '12 at 11:56
0

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

Community
  • 1
  • 1
Dany
  • 179
  • 1
  • 2
  • 10
0

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.

Gangnus
  • 24,044
  • 16
  • 90
  • 149
0

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.

Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84