How to finish() an Activity when Home button pressed

16,832

Solution 1

what about

android:launchMode="singleTask"

or

android:launchMode="singleInstance"

in your manifest? i think singleTask is the one you want, but im still not crystal clear on what you are doing.

"The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one." singleTask

@Override
void onPause() {
   super.onPause();
   finish();
}

dev docs: Acitvity Lifecycle , Finish

Solution 2

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

<activity
            android:name="MainActivity"
            android:exported="true"
            android:clearTaskOnLaunch="true"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
Share:
16,832

Related videos on Youtube

Yossi
Author by

Yossi

By Day - product manager @ NetApp ; By Night - dad and product hacker.

Updated on July 30, 2022

Comments

  • Yossi
    Yossi almost 2 years

    For a complicated reason I need to be able to finish() my activities when the user presses the HOME button.

    The story here is that I have a homescreen widget that launches a different part of my application that has a completely transparent activity (so the homescreen keeps showing even though my activity is running). If the previous activities were terminated via Home button, they are brought to the foreground and obscure the home screen.

    Or as alternative, can I have the new activity somehow force finish() the previous activity?

    • CommonsWare
      CommonsWare
      "I don't want users opening other Apps" -- why is this is good for the user? "it also must be an activity as I must make some calls that are available only on the UI thread." -- such as? The only such calls I can think of relate to activities and widgets themselves.
  • eternalmatt
    eternalmatt about 13 years
    This would finish the activity in which you wrote the line of code finish();. @Yossi, perhaps send an intent that you can interpret to mean it's time to call finish. Just thinking out loud.
  • Yossi
    Yossi about 13 years
    Thanks - but this would also finish my activity when I spawn a child activity - so I would need to maintain a flag for whenever I spawn child activity - clunky.
  • Yossi
    Yossi about 13 years
    Sending an intent got me thinking! What if the pendingIntent used by my widget included the flags referenced in link - that may blow away lingering activities. I will try it out.
  • Yossi
    Yossi about 13 years
    Didn't work - because the flags in the link require API 11 - making this useless for 99% of devices in the market today ...
  • Yossi
    Yossi about 13 years
    Worked it out!!! This answer was the closest, so I'll accept it. I added a finish() to onUserLeaveHint and made sure that all calls to startActivity from this activity included flag Intent.FLAG_ACTIVITY_NO_USER_ACTION so that onUserLeaveHint() only gets called when user really leaves my activity. These Android platform guys thought of everything!
  • laalto
    laalto over 9 years
    Please don't spam the site with unrelated youtube links.