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?

Yossi
  • 1,226
  • 1
  • 16
  • 31
  • I recommend getting rid of the transparent activity, so people won't be tempted to refer to your application as spyware. – CommonsWare Jun 17 '11 at 16:55
  • 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. – Yossi Jun 17 '11 at 17:17
  • 1
    "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. – CommonsWare Jun 17 '11 at 17:23
  • 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. – Yossi Jun 17 '11 at 18:31

5 Answers5

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

j0k
  • 22,600
  • 28
  • 79
  • 90
dylan murphy
  • 1,370
  • 1
  • 18
  • 33
  • 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 Jun 17 '11 at 18:29
  • 2
    Worked it out!!! This answer was the closest, so I'll accept it. I added a finish() to [onUserLeaveHint](http://developer.android.com/reference/android/app/Activity.html#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! – Yossi Jun 18 '11 at 14:48
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>
laalto
  • 150,114
  • 66
  • 286
  • 303
Quang Long
  • 31
  • 3
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(); 
 }
tangqiaoboy
  • 1,476
  • 1
  • 15
  • 32
JDK
  • 1
  • 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? – Yossi Apr 10 '12 at 09:59
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.

JDx
  • 2,615
  • 3
  • 22
  • 33
  • 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. – eternalmatt Jun 17 '11 at 16:07
  • Sending an intent got me thinking! What if the pendingIntent used by my widget included the flags referenced in [link](http://stackoverflow.com/questions/5979171/clear-all-activities-in-a-task/5979351#5979351) - that may blow away lingering activities. I will try it out. – Yossi Jun 17 '11 at 18:55
  • Didn't work - because the flags in the link require API 11 - making this useless for 99% of devices in the market today ... – Yossi Jun 18 '11 at 14:41
-1
@Override
public void onStop() {
    super.onDestroy();
}