Android Activity remove from stack - android

I want to completely remove an Activity from the Task Stack, but finish() only seems to minimize the Task. If I click on the button on my phone that browses all the open Tasks, it's still there. I would need to swipe it away to get rid of it.
How do I actually get rid of an Activity completely? Currently I have Activity_A that launches Activity_B. When I press back, Activity_B minimizes and Activity_A is brought to the front. How do I make it simply get rid of Activity_B and return to Activity_A?
EDIT:
I found the reason, Activity_B had Activity_A as the parent callback Activity. If you do not launch a new activity when A calls B, then it works properly (killing B kills the whole thing, A doesn't resurface).

Check is this what u need?
<activity android:name=".MyActivity"
android:noHistory="true">
</activity>

Have you tried:
intent = new Intent(view.getContext(),MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);

What I ended up doing was that when Activity_A calls up Activity_B, it doesn't create a new Activity but instead just replaces it. So then when I get rid of B (by swiping it away, for example), it doesn't go back to A.
Otherwise, B will have a callback to A then basically brings A back to the front when you get rid of B.

Related

After closing activity Android goes back to activity that should no longer b in the stack

I have activity A->B in the stack, and to launch activity C, I call
Intent starter = new Intent(context, MainActivity.class);
starter.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(starter);
This all works fine, Activity A and B both have OnDestroy called. If I press the hardware 'back' button now, the activity appropriately finishes and is hidden. The problem is now however, if I return to the application through the application by clicking the hardware recent apps button, it will return to Activity A. Activity was destroyed and not in the stack. In the manifest, none of activities have had a android:launchMode set, so they are on default.
The only other possible piece of relevant information is that there is an Activity X that is a launcher Activity that is android:launchMode="singleInstance" and it launches activity A, that being said, it gets destroyed and it shouldn't be in that activity stack anyways.
While pressing back button While in Activity C may called onDestroy() of actvity C.
please insert logs to see whether it is called or not. This is the only reason why your activity A launch again.
please refer Android Back button calls ondestroy?
please let me know if these not work for you.
The hardware back-button can be overwritten by the follwing code :
#Override
public void onBackPressed() {
//put Intent to go back here
}
You could just overwrite it with your code written above

Exact activity sequence in app (Android history stack)

And again about Android activities back stack magic. So I have two activities A→B (playlist A and player B), and I want the app should always close when Back pressed in A. But it doesn't.
Both activities launches as singleTop, B also has android:excludeFromRecents="true" and defined A as parent. B goes back to A with following flags Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK.
Everything works fine except the next case:
A→B,
Home pressed,
Launch app again from desk and got A opened,
Press Back button,
Here app should be closed but B shown again.
Any suggestions? Thanks!
When you are using the Intent.FLAG_ACTIVITY_NEW_TASK , it means a new instance of the activity is launched. You may have to use Intent.FLAG_ACTIVITY_SINGLE_TOP flag with single task option in manifest.

Android How to put activity in the background but not in stack

I have activity A and activity B. When app opens activity A opens.
I go to A to B. Then how do you "go back to A from B", without destroying B so that when you go from A to B the process is super fast? (Also how would you have to open old B and not create a new B?)
Also one more question, when you "go back to A from B", is there a way you prevent B to show up when you press back button once you are back in A?
When creating B, do all the hard work in onCreate, so later when resuming it, it starts up faster. B is destroyed only when it's not used and android needs to free some memory. Since it's in the background, switching to it will be pretty fast. To speed it up you can switch without an animation:
Intent intent = new Intent(this, B.class);
startActivity(intent);
overridePendingTransition(0, 0);
To prevent multiple B activities to be created, in your manifest's B activity tags set:
android:launchMode="singleTask"
After you go back from B to A, going back again, will quit the app. That is unless you go to A explicitly. Then you will want to override onBackPressed() in activity A, that always quits the app instead of going back to B.
Think you need to read this: http://developer.android.com/training/implementing-navigation/ancestral.html
If you want to always exit your app when pressing back in A, then you can override 'onBackPressed'.
Regarding how you would keep A 'alive' so to speak, you could set A to be 'singleInstance' or 'singleTask' in your manifest Android singleTask or singleInstance launch mode?

Android reuse activity from activity stack and clear it

I have application with main activity and some more.
On each other activity there is the logo of the application.
When user presses the logo button, I want to get back to the main activity.
I do not want to create new intent, since activity aready on the activity stack.
How can I get it - use the one on the stack?
How can I clear the whole activity stack, so back button will actually exit from the application instead of getting back to the previous activity?
Yoav
I do not want to create new intent, since activity aready on the activity stack.
If you start an activity (via intents or any other way) which was already started and is on the stack , then Android just takes that same instance of the activity and places it on top of the stack. A new instance is not created. Ofcourse this happens if you did not manually kill the activity (by calling finish() in it).
How can I clear the whole activity stack, so back button will actually exit from the application instead of getting back to the previous activity?
Its not recommended to override the back button to quit the application in every activity(Unless your app has strong reasons to do so). generally the app should let the user go back to the previous activity when he presses the back button (which is what a user might be expecting).
If you still would like to quit with the back button then you can override the back button function and launch the intent that leads to the home screen:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
I faced a somewhat similar problem. The following link might be helpful for you:
clearing stack of activities with just one press
Its very simple.
Don't call finish() on Home/Main activity.
For Ex: Say you have 4 activities.. If your requirement is like this .. Act1-->Act2-->Act3-->Act4-->Act1 . So, don't call finish() on Act1. But call finish() on Act2, Act3 while you are going to other activity. So when you click on logo in Act4, just call finish(). So, automatically you will come back to Act1 which is your Main activity.
If you have logo in Act2, Act3 also then call finish() on click of logo to go back to Main. But remember to call finish() on Act2 while you are going from Act2 to Act3

How do I clear all Activities from the stack?

I am having trouble popping all activities off the stack using Intent.FLAG_ACTIVITY_CLEAR_TOP and android:launchMode="singleInstance".
In my application activity A, launches activity B (via startActivity) which in turn launches activity C (via startActivity). On activity C the user presses a menu item to return to activity A. When they arrive at activity A, I want only A on the stack such that if they click the back button they return to the home screen (desktop).
This is the code that I am currently using when the user presses a button to return to A:
Intent i = new Intent(this, A.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
All activities are defined as android:launchMode="singleInstance" in the project manifest.
My code doesn't seem to work though. Once I'm back on activity A if I click the back button I return to activity C. Am I misunderstanding how to use Intent.FLAG_ACTIVITY_CLEAR_TOP?
I've always found the best way to ensure C would be removed from the stack is to call finish() after startActivity to remove C from the stack.
The documentation does read as though things would behave the way you expected them to, but it would seem this isn't happening, so finish() will ensure C is removed.
I usually use the technique Al suggested (calling finish() after starting the new activity).
You could also experiment with task affinity. I've never done that myself, but it may be relevant in your case as well. See this thread: http://groups.google.com/group/android-developers/browse_frm/thread/ca3b26a14d024597/129e37375105901b

Categories

Resources