0

Well, my question is just as simple as in the title:

Is there a way to call finish() on all running activities inside application ?

So i need a way to completly shut down my app.

Adam Varhegyi
  • 11,307
  • 33
  • 124
  • 222
  • [How to close an application](http://www.google.com.pk/search?q=android+how+to+close+application+programmatically) – Adil Soomro Jul 11 '12 at 14:25
  • You can check Dave Webb's answer [here][1]. [1]: http://stackoverflow.com/questions/2042222/close-application – M.ES Jul 11 '12 at 14:27
  • http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon/2034238#2034238 – CommonsWare Jul 11 '12 at 16:23

3 Answers3

1

You can achieve that with a BroadCastReceiver:

In every activity put:

private BroadcastReceiver mLoggedOutReceiver = new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
       finish();
  }
};

Register receiver in onCreate:

registerReceiver(mLoggedOutReceiver, new IntentFilter(Preferences.INTENT_ACTION_LOGGED_OUT));

on onDestroy:

@Override
public void onDestroy() {
    super.onDestroy();
    unregisterReceiver(mLoggedOutReceiver);
}

on manifest for every activity and the following filter:

<intent-filter>
    <action android:name="com.example.intent.action.LOGGED_OUT" />
</intent-filter>

I have the intent Action declared in Preference class like this:

public static final String INTENT_ACTION_LOGGED_OUT = "com.example.intent.action.LOGGED_OUT";//logout

And finally all you have to do is call sendBroadcast from an exit button or something:

sendBroadcast(new Intent(Preferences.INTENT_ACTION_LOGGED_OUT));//logout

Hope this helps.

Chronos
  • 1,972
  • 17
  • 22
  • Sadly it is not worked for me :( Still a bunch of activity running when i clicking menus like an idiot user. – Adam Varhegyi Jul 12 '12 at 07:52
  • You add the mLoggedOutReceiver on all activities? – Chronos Jul 12 '12 at 13:16
  • Yes, well my problem really is: If i clicking too fast for menus i got multiple activites running by the same menu. And if i close one via exit, there can be still running activities. Thats why im searching for a code snippet that completly shut down the whole app with all running activities. (SORRY FOR BAD ENGLISH) – Adam Varhegyi Jul 13 '12 at 12:02
  • Thats weird, this code works for me killing all open activities after logout. I don't understand how works your app, is an app with a menu that launch other activities or you have a TabHos? If a menu just call finish() after selecting an option, that way you kill the current activity and open the new one. – Chronos Jul 13 '12 at 15:32
0
android.os.Process.killProcess(android.os.Process.myPid());

this code can shut down your app.

jaisonDavis
  • 1,407
  • 2
  • 22
  • 36
  • You will also kill any services by doing this so make sure that is what you want. Still not a recommended practice. – Morrison Chang Jul 11 '12 at 14:34
  • 1
    This is not likely to have the desired result. An app with more than one activity in the task stack is likely to be immediately, automatically restarted from the preceding activity, which is presumably not what the asker wants to have happen. – Chris Stratton Jul 11 '12 at 14:40
0

NO

but you can exist hole application like System.exit(0); but it is not the recomanded way..as android not design to exit the application.

Mohsin Naeem
  • 12,542
  • 3
  • 39
  • 53
  • 1
    Not really - although it terminates the current process, it also results in an automatic restart of a new process to host any activity in the task stack preceding the one that called it. – Chris Stratton Jul 11 '12 at 14:42