the app must be quit in the first activity (MainActivity) in the stack (usually the first activity which is lauched when the app starts).
I'm using this code:
finish();
android.os.Process.killProcess(android.os.Process.myPid());
both lines are important. Only killing is not sufficiant since Android may start your app again automatically, therefore finish() must also be used.
If you want to quit your app from another activity, first go back to the MainActivity and quit it from there.
To go back I use this code:
Intent i = new Intent(context, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(i);
This will call onCreate of the MainActivity. Using a static variable (in the application context) I set a flag if I want to quit in onCreate or not.