How to finish() an Activity when Home button pressed
Asked Answered
H

5

9

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?

Hungnam answered 17/6, 2011 at 15:50 Comment(4)
I recommend getting rid of the transparent activity, so people won't be tempted to refer to your application as spyware.Sunflower
Thanks Mark; there is a good reason for this transparent activity. I show animation on my homescreen widget for the short duration of the transparent activity (it self destructs after 6 seconds) and I don't want users opening other Apps, so this also serves to "lock" the homescreen; it also must be an activity as I must make some calls that are available only on the UI thread.Hungnam
"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.Sunflower
My Widget is triggering a reconfiguration of the device radios and some screen settings such as brightness, which takes a few seconds. I want the user to not start anything else while this change is happening. Brightness changes (and others) must be made on the UI thread.Hungnam
K
12

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

Kreit answered 17/6, 2011 at 16:7 Comment(2)
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.Hungnam
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!Hungnam
H
3

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>
Hornwort answered 23/10, 2014 at 1:35 Comment(1)
Please don't spam the site with unrelated youtube links.Langur
B
0

Not sure about finish() on home button press but i think you can finish() the previous activity using:

Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivity(i);
finish();

Probably not the best way of doing it though. I think you can also call the parent activity of a subactivity and finish it that way but not sure.

Burkes answered 17/6, 2011 at 16:1 Comment(3)
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.Lumper
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.Hungnam
Didn't work - because the flags in the link require API 11 - making this useless for 99% of devices in the market today ...Hungnam
O
0

I had the problem with closing the sound on home button is pressed. I did this code below. Hope it wil help you. Override onpause() method.

 @Override
 public void onPause(){
      System.exit(0);
      super.onPause(); 
 }
Osmanli answered 10/4, 2012 at 5:21 Comment(1)
Thanks ... I would be concerned about using non-Android framework calls. What if it blows away something (such as file pointers) still needed for my App shutdown?Hungnam
D
-1
@Override
public void onStop() {
    super.onDestroy();
}
Dayflower answered 20/2, 2015 at 16:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.