iOS-Coder
iOS-Coder

Reputation: 1241

Can I exit my android app with code when it is started from a deeplink?

I am building an app which uses either some activities in which the user can navigate or uses one specific activity which starts the app when a deeplink is used. In this DeeplinkActivity I have a button with which the app should exit the entire app and not put it into the background. I tried the following code, which only closes the app, but it still remains in the background?!

public void onStopButton(View view) {
    log.info("UI -> Stop this app");
    // this.finishAffinity();
    android.os.Process.killProcess(android.os.Process.myPid());
    // finish();
    System.exit(0);
}

I tried all kinds of suggested combinations, but none of them really exits the app. Anybody have some other (working) code suggestions? I have set minSdkVersion=19 and targetSdkVersion=26

Upvotes: 1

Views: 492

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006674

First, note that having a button such as you describe is considered to be an anti-pattern in Android.

That being said, if your minSdkVersion is 21 or higher, replace your onStopButton() with:

public void onStopButton(View view) {
  finishAndRemoveTask();
}

If your minSdkVersion is lower than 21, there are some clunky workarounds that I do not recommend.

Upvotes: 0

Related Questions