Android - How To Override the "Back" button so it doesn't Finish() my Activity? Android - How To Override the "Back" button so it doesn't Finish() my Activity? android android

Android - How To Override the "Back" button so it doesn't Finish() my Activity?


Remove your key listener or return true when you have KEY_BACK.

You just need the following to catch the back key (Make sure not to call super in onBackPressed()).

Also, if you plan on having a service run in the background, make sure to look at startForeground() and make sure to have an ongoing notification or else Android will kill your service if it needs to free memory.

@Overridepublic void onBackPressed() {   Log.d("CDA", "onBackPressed Called");   Intent setIntent = new Intent(Intent.ACTION_MAIN);   setIntent.addCategory(Intent.CATEGORY_HOME);   setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   startActivity(setIntent);}


It was easier to implement it only with one line of code:

@Overridepublic void onBackPressed() {   moveTaskToBack(true);}


simply do this..

@Overridepublic void onBackPressed() {    //super.onBackPressed();}

commenting out the //super.onBackPressed(); will do the trick