Questions tagged [activity-stack]

Activities can open other activities. Each opened Activity is put on top of the opener. This stack is called BackStack in Android. You can navigate to the previous Activity by pressing the device's back button.

An application usually contains multiple activities. Each activity should be designed around a specific kind of action the user can perform and can start other activities. For example, an email application might have one activity to show a list of new email. When the user selects an email, a new activity opens to view that email.

An activity can even start activities that exist in other applications on the device. For example, if your application wants to send an email, you can define an intent to perform a "send" action and include some data, such as an email address and a message. An activity from another application that declares itself to handle this kind of intent then opens. In this case, the intent is to send an email, so an email application's "compose" activity starts (if multiple activities support the same intent, then the system lets the user select which one to use). When the email is sent, your activity resumes and it seems as if the email activity was part of your application. Even though the activities may be from different applications, Android maintains this seamless user experience by keeping both activities in the same task.

A task is a collection of activities that users interact with when performing a certain job. The activities are arranged in a stack (the "back stack"), in the order in which each activity is opened.

The device Home screen is the starting place for most tasks. When the user touches an icon in the application launcher (or a shortcut on the Home screen), that application's task comes to the foreground. If no task exists for the application (the application has not been used recently), then a new task is created and the "main" activity for that application opens as the root activity in the stack.

When the current activity starts another, the new activity is pushed on the top of the stack and takes focus. The previous activity remains in the stack, but is stopped. When an activity stops, the system retains the current state of its user interface. When the user presses the Back button, the current activity is popped from the top of the stack (the activity is destroyed) and the previous activity resumes (the previous state of its UI is restored). Activities in the stack are never rearranged, only pushed and popped from the stack—pushed onto the stack when started by the current activity and popped off when the user leaves it using the Back button. As such, the back stack operates as a "last in, first out" object structure.

Reference: http://developer.android.com/guide/components/tasks-and-back-stack.html

137 questions
232
votes
14 answers

How to prevent going back to the previous activity?

When the BACK button is pressed on the phone, I want to prevent a specific activity from returning to its previous one. Specifically, I have login and sign up screens, both start a new activity called HomeScreen when successful login/signup occurs.…
ecem
  • 3,574
  • 3
  • 27
  • 40
130
votes
11 answers

How to prevent multiple instances of an Activity when it is launched with different Intents

I've come across a bug in my application when it is launched using the "Open" button on the Google Play Store app (previously called Android Market). It seems that launching it from the Play Store uses a different Intent than launching it from the…
bsberkeley
  • 1,301
  • 2
  • 9
  • 3
76
votes
13 answers

Android: bug in launchMode="singleTask"? -> activity stack not preserved

My main activity A has as set android:launchMode="singleTask" in the manifest. Now, whenever I start another activity from there, e.g. B and press the HOME BUTTON on the phone to return to the home screen and then again go back to my app, either via…
znq
  • 44,613
  • 41
  • 116
  • 144
30
votes
5 answers

Prevent Activity Stack from being Restored?

When an application's process is killed, its activity stack is saved. Then when the application is restarted, all my activities resume and run into null pointers. Rather than modify every activity to accommodate this event, I would rather just have…
Nathan Fig
  • 14,970
  • 9
  • 43
  • 57
26
votes
9 answers

how to finish all activities and close the application in android?

My application has the following flow: Home->screen 1->screen 2->screen 3->screen 4->screen 5>Home->screen 2->Home->Screen 3 My problem is that when I am trying to close the application then Home activity opens everytime when I am trying to close…
rahul
  • 2,613
  • 8
  • 32
  • 55
24
votes
4 answers

noHistory vs finish() - Which is preferred?

I don't want my application to show few Activity (say SplashScreenActivity) when pressing back button. So I've used noHistory=true in my Manifest.xml for that Activity as show below:
Gokul Nath KP
  • 15,485
  • 24
  • 88
  • 126
21
votes
8 answers

How to find back stack activities in an android application?

I have an application with activities back stack A -> B -> C -> D -> E. Now at activity E, I want to know the back stack activities that I navigated from. How do I find this??
azzits
  • 345
  • 2
  • 3
  • 10
12
votes
3 answers

Activity restarts on Force Close

I have an Application with a single root Activity. I've recently had it brought to my attention that any kind of Force Close on my Activity results in it restarting and I have no idea why this might happen. If I force an uncaught exception or use…
Zulaxia
  • 2,702
  • 2
  • 22
  • 23
10
votes
3 answers

How avoid returning to login layout pressing the back button/key?

I want create an app for my institute. The problem is: my application will have two layouts (login and dashboard). Students can correctly fill out the login form, enter the dashboard, press buttons, and fill other fields. But if the user then…
SoldierCorp
  • 7,610
  • 16
  • 60
  • 100
9
votes
3 answers

Android: keep task's activity stack after restart from HOME

My application has two activities, that I start in this order: HOME > A > B Now I press HOME and launch "A" again. I would like to see activity "B" on a top of "A", but instead I get "A" - so the activity stack is cleared. Manifest:
alex2k8
  • 42,496
  • 57
  • 170
  • 221
8
votes
1 answer

How to see the activity stack in debug?

I have a problem that one of my activities is popping out after I think I finished it. Is there a way to see the stack of the activities?
AlikElzin-kilaka
  • 34,335
  • 35
  • 194
  • 277
8
votes
1 answer

onCreate always called if navigating back with intent

I have an activity called HomeActivity that has a SurfaceView and shows a camera preview picture. This activity is quiet heavy and feels slow if you are starting/restarting it. So I made some investigations and found out, that somehow always the…
Mark
  • 7,507
  • 12
  • 52
  • 88
6
votes
1 answer

Finish All Instance of particular Activity

There can be many activities in application and the last launched activity stay on top of stack and on pressing back it finish the current activity.I have a sequence of Activity and here is the flow .. if we have A,B,C(1),D,C(2)... Activity C(1) and…
Vipin Sahu
  • 1,441
  • 1
  • 18
  • 29
6
votes
4 answers

How to Control Android back stack

Lets say I have A->B->C->D->E In android back stack. I want to be able to get back to one of the following: A->B->C A->B A How can I achieve this? Hopefully without forcing back button clicks.
Vlad
  • 735
  • 2
  • 10
  • 19
5
votes
3 answers

Cannot set both FLAG_ACTIVITY_SINGLE_TOP and FLAG_ACTIVITY_CLEAR_TOP in one intent?

I'm using flag FLAG_ACTIVITY_SINGLE_TOP and FLAG_ACTIVITY_CLEAR_TOP to go back to my previous "standard" activity. I use FLAG_ACTIVITY_SINGLE_TOP to prevent re-creating a new instance. But what I found is that the flag FLAG_ACTIVITY_SINGLE_TOP is…
Derek Hsu
  • 1,158
  • 11
  • 15
1
2 3
9 10