Is Home Activity instantiated multiple times? - android

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.

Related

What exactly does moveTaskToBack() do?

From the documentation:
public boolean moveTaskToBack (boolean nonRoot)
Move the task containing this activity to the back of the activity stack. The activity's order within the task is unchanged.
What exactly does "Move the task containing this activity to the back of the activity stack" mean? I know that each task is a stack of activities, but according the the above sentence, it seems there is also a global stack of tasks as well?
When I try this method out, the current activity moves to the background, and the behaviour seems very much like when clicking the Home button (e.g. the activity is not destroyed and can be resumed later). Is there any difference between calling this function and pressing the Home button?
There is not a "global stack of tasks." There's a global stack of activities, which can be from one app or from multiple. Let's say you have an app where you can click a link, bringing you to your browser. If the browser then calls a moveTaskToBack() method, then the original app activity will open, with the previous activities on the backstack still in place.
Now imagine instead of calling the moveTaskToBack() method, the user presses the Home button. Now, pressing Back on your phone will not take you back to the original app. You'll just stay on the home screen.

Difficulty understanding back stack and tasks

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.

Android; How can I detect whether a Parent activity is still alive in Activity stack and then receate it

I have activities in sequence as Activity A calling Activity B,When I am on Activity B I press Home button and start another Application (for example Map). If i stay on this second application for a long time say 5-10 minutes and then press Home Button again and choose to Start my application again, then Activity B is started again and works fine. But when i Press Back key - I return to my Activity A again (which is also correct) but it shows a Blank Screen. Ideally in correct version it should show me Acitivty A with the XML data is ListView form.
Alternatively, in the above description, when the other Map Application is started and if the user stay on it only for 1-2 minutes then the above problem doesnt not occur.
Can anyone suggest a solution on the same.
Is it that i need to check in Activity B whether Activity A is still there in Activity Stack (How do i do the same) and If its not in my Activity stack then re-create it.
I tried doing alwaysRetainTaskstate in my Android manifest file for Activity A. But it doesnt work at all
You don't have to do any of that, Android takes care of the technicalities for you - it will recreate your Activity A.
You just need to remember to save A's state when B is opened (take a look at Activity.onSaveInstanceState). When A is restarted, your saved state will be passed to onCreate.
You should really read about Activity Lifecycle
This should help.

android, starting and exiting activities

I have not really understood the handling of activities and the stack.
I have 3 activities, A - a splashcreen, B- a menu and C another Activity. I start the splash and exits it after a while when the menu is started.
In code I handle them all like this:
startActivity(new Intent(this, ContactInfoMenu.class));
finish();
Now, if I start the app and goes A-B-C, when I hit "Back" in C screen I jump back to B-the menu. Another "Back" exits the application, just like I want.
BUT .. if I go A-B-C-B-C - the "Back" button in C screen exits the whole app instead of getting me back to the B screen?
Why is that? It does like that in all my "subscreens", I can only enter them once, if I enter them a second time the "Back" button exits the app. And I have not tried to catch the "Back" action anywhere? Shouldn't I always call "finish()" when I start a new activity?
Regards
Finish is good for leaving the current activity and going back to the previous one. Otherwise, try to avoid calling finish() if you can help it.
There are a set of flags that you can pass when you start an activity that do a better job of determining how that activity behaves on the stack. These include:
FLAG_ACTIVITY_NO_HISTORY - your activity will not remain on the stack after another activity covers it.
FLAG_ACTIVITY_CLEAR_TOP - a good way to pop off a bunch of activities when you need to "go back" to a certain activity.
Many of these flags can be set in the manifest. Reading up on them will give you a better idea about "The Android Way".
Basically, You don't need to call finish() every time you go to another activity. If system is low on memory it will close your activity instance by itself.
finish() is more often used when yor are inserting some information in one page and then moving on to some other page. In that case, you might need to fininsh your first activity.
But in case where you need to shuffle between views, you must not use a finish() function, because it will cause the whole application to finish.
Try using back button of your own in your views to shift between activities, where you can move to any other activity of your application or even to the Main Screen.

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