Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to finish() an Activity when Home button pressed

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?

like image 206
Yossi Avatar asked Jun 17 '11 15:06

Yossi


2 Answers

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

like image 101
dylan murphy Avatar answered Sep 20 '22 08:09

dylan murphy


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>
like image 24
Quang Long Avatar answered Sep 18 '22 08:09

Quang Long