How does android:noHistory="true" work? - android

Lets say I have a base activity with a menu, when I click on menu item A, it goes to activity A. I open the menu again, and go to B. From B I go back to A, and back and fourth like this for a while.
So the stack would be A, B, A, B, A, B, ....
And when I hit the back button, it goes backwards through the stack as expected.
However lets say I don't want this functionality, so I add to my manifest, android:noHistory="true". So when I hit the back button it exits the application instead of going though the stack.
Now the illusion makes it seem, lets say if I'm in activity A, I use the menu and go to activity B, the stack would just be B, because I can't go back to A.
But, when using noHistory="true", does the true stack of A, B, A, B, A, B exist? Rather, is every call to an activity by using the menu instantiating a new copy of that activity, but the user can't see it? Would this be causing resource issues?
Or when noHistory="false", does the back button just call something like startAcitvity(intent) again or is it going through each new copy that was instantiated?
I'm concerned with resource issues and not slowing down a users android device.

From the docs about noHistory:
A value of "true" means that the activity will not leave a historical trace. It will not remain in the activity stack for the task, so the user will not be able to return to it.
Regarding your question:
does the true stack of A, B, A, B, A, B exist?
The docs would indicate no.
I'm concerned with resource issues and not slowing down a users android device.
You really don't need to worry about this. The OS should handle the cleanup of activities when memory is getting low. Its more likely that poor use of bitmaps or logic in your activities will result in performance slowdowns.

android:noHistory=“true” works :-
Let suppose you have opened "your app".
You are on homepage Activity now,
After it you go to the another(second) activity.Here from second activity you press the home button of mobile device or open the some other application.
Now again if you open "your app" it will go to the homepage of app instead of going to the activity which one you left the app(i.e.second activity).

I had few fragments in my app and it seemed difficult to get out to the home screen by pressing back button without entering Launcher Activity of my app. I used android:noHistory="true" in the manifest of the launcher Activity of my app and the problem gets solved now.

Related

Keep one activity in the bottom of the backstack

I want to learn the proper way to manage the activity back stack with regards to my issue. Most of the time when a person uses my app, I want to keep an activity in the bottom of the stack, let's call this Activity A. This would be their "Home" activity. I have a navigation view which can take the user to a bunch of other activities, but I want to manage what displays when they tap back. I want Activity A to always be the last activity in the stack, so the stack can look like A -> B -> C-> D, and when the user is on Activity D and they want to go to Activity E, I want the stack to look like A -> E when they press it.
A possible solution I have found is by clearing all the activities in the current stack, launching Activity A, and sending an intent for Activity E in the intent I launch Activity A with, then that will just check it's intent extras and if it finds an intent in the extras it would just launch that intent. This results in the stack looking the way I want, Activity A -> Activity E. I just want to know if there is a better or simpler way.
I have tinkered with the activity properties in my manifest, but it seems like I can't do exactly what I would like to with those.
Any help would be appreciated :)
Lets assume you want to keep activity A in the back stack so that whenever a user presses back, you want to show activity A on top. Lets say you go to B from A and then C from B. So whenever you go from any activity(other than A) to any other activity just call finish() from the calling activity, this will remove the stack entry of the corresponding activity, ensuring that only activity is there in the back stack.

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.

How can i programmatically show activity on the top of task stack?

My application start foreground service witch keep connection to server. It's show notification with pendingIntent witch show MainActivity. When i (user) tap on application icon (on desktop or application list) it's show "task stack". I mean if was lunched MainActivity it shows it, if user go to activity B or C (or lunch some other activities) it shows it (i mean top activity from task stack). There is a problem - if user tap on notification he see again MainActivity (on the top of the stack) but i expect top of tack stack (activity B,C or other witch was lunched by user at the end).
Half solves when i set attribute for MainActivity "singleTask", now it's always root of task stack, BUT a'm loosing all activities (B, C and other wich user lunched). Solution like in Reuse Activity on Top of Stack simular, but i need only one activity at root.
Maybe my logic is wrong and i need some another way to resolve this problem. But i want to know how can i programmaticly show task stack (top activity) like application icon does?
The documentation Tasks and Back Stack describes how to handle navigation correctly.
In short, if users tap on your navigation and you take them to an Activity in your app, when they click Back they should up in your app's Activity hierarchy until they reach the Home screen. They should never go to the stack in another task. That is, if they're in your app in Activity C, and you send a notification which they click that takes them to Activity A, then clicking Back should take them to the parent of A, and not to C. If they want to go to C, they can use Recents.
On older platforms, Recents isn't available.
This is by design.
To construct the proper synthetic back stack, use TaskStackBuilder

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.

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