Reproducing the option to force close

45 Views Asked by At

In android there is option in
Settings->application->your app-> force close

Does anyone know how does it work programmatically?
I tried a lot of things to close my app and those not working properly.

And those shutting down app completely with all threads/services.

Anyway I need something what close my app with threads sevices and everything.

 public void turnOffApp() {
    try {
        for (IThreadController thread : Objects.threadList) {
            thread.stopThread();
        }
        Log.d(Objects.context.getString(R.string.complete), Objects.context.getString(R.string.turningOffThreads));
        finish();
    } catch (Exception e) {
        Log.d(Objects.context.getString(R.string.error), Objects.context.getString(R.string.throwErrorWhileStoppingThreads), e);
    }
}

Objects.threadList is a globalObject where i store all my created threads. Btw is fire also when app closeing or crashes and then it's work corectly but i want to have it also in button and here isn't work so well

1

There are 1 best solutions below

5
Sahil On

There is no direct way to kill application, though you can achieve this by let say on pressing a button you load First activity and clear all other activities in stack. Now you are left with one activity to deal with, to finish that you can do as following :

Intent intent = new Intent(getApplicationContext(), FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);

The above code removes all activities from stack except for FirstActivity. To finish the FirstActivity enter the below code in Firstactivity's oncreate

if (getIntent().getExtras() != null && getIntent().getExtras().getBoolean("EXIT",false)) {
finish();
}

Hope it helps!