12

I have an app which has 4 activities in it.Within app, history activities, i.e. Activities from where I navigated should not be destroyed , so I don't call finish() when I am navigating.

But when I press HOME button I want to kill all activities , So that When I come back to the app , Index screen or say first activity is displayed instead of previous paused activity.

Problem here seems to be, how to differentiate between backs within an app to HOME button.

I saw few answers regarding this in other questions. Got more confused.
Is there a way other than intercepting HOME KEY PRESS, because as suggested in other threads,
I should not override HOME key press (as it might have side effects)

sat
  • 40,138
  • 28
  • 93
  • 102

2 Answers2

20

Set android:clearTaskOnlaunch="true" on the activity launched from the home screen.

You might also check some of the other attributes you can specify on activity, to tweak it's behavior a bit more.

Franci Penov
  • 74,861
  • 18
  • 132
  • 169
  • Thats perfect ! I have to read documentation properly I guess , I think I have read only 0.1% of documentation ! Its huge ! Thankyou. – sat Mar 15 '11 at 06:17
  • I am trying to accept from past 4 min . It says I have to wait for another 25 sec more before I could accept your answer ! New rules I suppose ! or I didn't know abt it :) – sat Mar 15 '11 at 06:25
14

On main activity add:

android:launchMode="singleTask" android:clearTaskOnLaunch="true"

On the others add:

android:finishOnTaskLaunch="true"

This way it will kill off any activity when returning to the app after being in background.

neteinstein
  • 17,529
  • 11
  • 93
  • 123
  • 1
    rather than posting the same answer multiple times, flag the questions as duplicates if they have the same response. It's much more useful to the community. – Mat Jun 07 '11 at 14:51