0

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

Ying Li
  • 2,500
  • 2
  • 13
  • 37
  • Could you check similar questions in stackoverflow, [here](http://stackoverflow.com/questions/1898886/removing-an-activity-from-the-history-stack?rq=1), [here](http://stackoverflow.com/questions/7992812/android-how-do-i-totally-remove-an-activity-from-the-activity-stack?rq=1) – a3.14_Infinity Oct 03 '15 at 07:02
  • Yeah, I have seen these two. The two main methods they mentioned I have tried. 1) android:noHistory="true" Doesn't actually work. 2) Launching another Activity and clear the Task. This DOES work but seems a bit hacky. There must be a way to simply end this Activity without having to launch a second one to end this one. – Ying Li Oct 03 '15 at 07:11
  • check [this](http://stackoverflow.com/questions/1898886/removing-an-activity-from-the-history-stack?rq=1). answer, its help me in this same question. – Sudhir Kumar Oct 03 '15 at 07:25

3 Answers3

0

Check is this what u need?

<activity android:name=".MyActivity"
    android:noHistory="true">
</activity>
Nivedh
  • 971
  • 1
  • 8
  • 19
  • I have seen that in similar questions, but it doesn't work. When I press "back", the Activity goes to the back of the Stack but doesn't disappear... – Ying Li Oct 03 '15 at 07:10
  • Not a good method but still try this.. Inside your on back pressed write Intent intent = new Intent(this, ActivityA.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(intent); – Nivedh Oct 03 '15 at 07:17
  • :D In my comment to the guy who commented on my question, I mentioned that this DOES work... It just seems very hacky. There's no native way for Android Activities to end itself? I suppose if that's the case, I would have to use the hacky way. – Ying Li Oct 03 '15 at 07:19
0

Have you tried:

intent = new Intent(view.getContext(),MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
Patricia
  • 2,885
  • 2
  • 26
  • 32
0

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.

Ying Li
  • 2,500
  • 2
  • 13
  • 37