Adam Varhegyi
Adam Varhegyi

Reputation: 9924

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

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.

Upvotes: 0

Views: 458

Answers (3)

Chronos
Chronos

Reputation: 1962

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.

Upvotes: 1

Mohsin Naeem
Mohsin Naeem

Reputation: 12642

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.

Upvotes: 0

jaisonDavis
jaisonDavis

Reputation: 1427

android.os.Process.killProcess(android.os.Process.myPid());

this code can shut down your app.

Upvotes: 0

Related Questions