1

I want to close my application when it goes background.

My current method is to call finish() in the OnPause() of one of the main activity.

However, in my application there are several activities. When it jump to another activity from my main activity, my main activity will be closed.

It's not what I excepted. I want my applications closed only when the entire applications goes to background (e.g. via Home key)

Thanks.

dong221
  • 3,390
  • 6
  • 29
  • 31

3 Answers3

0

You don't have to do that.. Android does it by itself when you press Home key.. and when it needs Resources... read this for more http://developer.android.com/reference/android/app/Activity.html

ngesh
  • 13,398
  • 4
  • 44
  • 60
  • Thanks, but I have to do that for a special reson. – dong221 Oct 20 '11 at 04:04
  • 1
    @dong221... Every time you navigate from 1 screen to other screen its onPause() method is called, sometimes onDestroy() method is called. on Home key press activity is finished by default, so there is no way you can do that yourself.. – ngesh Oct 20 '11 at 04:13
  • There are instances where this must be done, such as secured apps running in the background. See, for instance, the Chase Bank app. Saying "don't do it" is not an answer, nor even close. – Ginger McMurray Dec 19 '19 at 20:19
-1

You can kill your own app's process using Process.killProcess(Process.myPid()). If you can't rely on a callback to you activity to tell you when your app has lost focus, and given that you can't rely on onPause (as noted above), you could in principle post yourself a suicide note during onPause:

_runnable = new Runnable() {
        @Override
        public void run() { 
            Process.killProcess(Process.myPid());
        };

_handler = new Handler().postDelayed(_runnable, 3000);

and then have one your other activities intercept and cancel it during onResume:

_handler.removeCallbacks(_runnable);

(You'll need a way to provide access to the variables across multiple activities.) If the cancelation never comes, then your app will eventually be killed.

Kludgy, but possible, I think.

stephent
  • 1,355
  • 15
  • 29
  • No. One finish()'s application components. One **does not** kill their process. If you do, Android will likely recreate it. – Chris Stratton Oct 16 '14 at 20:28
-1

Now lets consider this is how ur manifest looks like. Main activity Hello starts Hello2 which starts Hello3 and so on.

       <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".Hello" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="Hello2"></activity>
    <activity android:name="Hello3"></activity>
    <activity android:name="Hello4"></activity>
</application>

Now create dummy Launcher activity(HelloStarter) which starts ur Hello activity. See updated manifest.

       <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".HelloStarter" android:label="@string/app_name"
        android:clearTaskOnLaunch="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="Hello"></activity>
    <activity android:name="Hello2"></activity>
    <activity android:name="Hello3"></activity>
    <activity android:name="Hello4"></activity>
</application>

In ur Hello activity's onDestroy() add this:

     System.exit(0);

This works....!!!!

Rohit
  • 593
  • 2
  • 8
  • No, exiting the process will not work - Android, not you, decided when a process should or shouldn't exist, and if you exit one it thinks should still exist, it may recreate it. – Chris Stratton Oct 16 '14 at 20:28