Exit completely from android app

SparrOw picture SparrOw · Jun 29, 2014 · Viewed 8.9k times · Source

I want to exit entire app by clicking exit button. Actually I am using exit button instead of log-out. So whenever user clicks on it, it should finish complete app but it destroys only last running activity. What should i do now? I am using this method in my code, any help will be appreciated, thanks in advance.

         public void exit(View v){
                finish();
                System.exit(0);
            }

I have also used following code but it kills only last running activity and not the complete application.

             public void exit(View v){
                android.os.Process.killProcess(android.os.Process.myPid());
                System.exit(1);
            }

Answer

Abhishek Verma picture Abhishek Verma · Jun 29, 2014

There is an alternative for otherwise killing your app completely. Minimize it and the Android system will kill it automatically when necessary. To do so,

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Furthermore, this SO question may help: How to close android app completely