How to exit an Android app programmatically?

Lemon Juice picture Lemon Juice · Jul 18, 2013 · Viewed 257.2k times · Source

I am sure this question has been asked number of times because I read a few. My client wants me to put a button into his app where users can click and exit. I have read this and found calling finish() will do it. But, finish is only closing the current running activity right? I have lot of activities so in this case I have to pass each and every activity's instance and finish them or make every activity into Singleton pattern.

I also got to know Activity.moveTaskToBack(true) can get you into the home screen. OK, this is not closing but backgrounding the process. So is this is effective?

Which method should I use to close the app completely? Any of above described or any other method/other usage of above methods?

Answer

Fayaz picture Fayaz · Apr 9, 2014

Actually every one is looking for closing the application on an onclick event, wherever may be activity....

So guys friends try this code. Put this code on the onclick event

Intent homeIntent = new Intent(Intent.ACTION_MAIN);
    homeIntent.addCategory( Intent.CATEGORY_HOME );
    homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
    startActivity(homeIntent);