0

Am running a simple app which will either On/Off Wifi when opened. Below is the code. I want the app to exit once the task is performed and so I have tried adding finish() or System.exit(0) at the end. It works correctly but when I check the overview button, I can still see the app in running state and I need to swipe it to stop the app. [I mean the third button which shows the running apps as thumbnails]

import android.content.Context;
import android.net.wifi.WifiManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;


public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
    if(wifiManager.isWifiEnabled()){
    wifiManager.setWifiEnabled(false);
    }
    else{
    wifiManager.setWifiEnabled(true);
    }
     finish();
}
}
Trooper
  • 75
  • 1
  • 8
  • Why do you care if it continues running? Is it actually that you just do not want it to appear in the recents list? http://stackoverflow.com/questions/3762763/how-to-remove-application-from-recent-application-list – weston Jan 21 '17 at 18:07

2 Answers2

1

Finish will end the Activity. That is not the same as ending the app. Exit() doesn't really exit the app either, and at any rate would be a dirty shutdown rather than a clean one (so it may cause issues with anything other than a trivial app). Android is really written to not allow you to do that.

However the overview doesn't just show apps that are running in memory. It shows any app that is or was running, and will restart it with the most recent intent if it was paged out of the set of running applications. So things are really working as you think they are, or as they do on a traditional computer. I don't particularly like it either, but you're better off learning and understanding it that fighting it.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Random currveball. Is there a way to kill the app process in Android – Peter Chaula Jan 21 '17 at 18:08
  • Not the last time I looked (which admittedly was around 2014). You can get yourself out of the recents menu with some work. But that doesn't mean Android won't have kept you in memory if it has sufficient and wouldn't just restart an Activity rather than do a full reload next time you're invoked (which means static variables may not be re-initialized but keep their old values). – Gabe Sechan Jan 21 '17 at 18:10
0

Use finishAndRemoveTask(), available since API 21. It will remove app from recent applications list.

ivan_onys
  • 2,282
  • 17
  • 21