Android Overriding onBackPressed()

Jilberta picture Jilberta · Aug 20, 2013 · Viewed 172.7k times · Source

Is it possible to override onBackPressed() for only one activity ?

On back button click I want to call a dialog on a specific Activity, but in all other activities i want it to work as it worked before (going to previous activities).

EDITED

Thank you everyone for your answers, I already had everything like you told me, but my problem was that when i was clicking back button on another Activity, I was going to my previous Activity (The one where i had back button Overridden) and i thought that it wasn't working, i thought it was overriding onBackPressed() in whole Application, now i got it.

Answer

codeMagic picture codeMagic · Aug 20, 2013

Yes. Only override it in that one Activity with

@Override
public void onBackPressed()
{
     // code here to show dialog
     super.onBackPressed();  // optional depending on your needs
}

don't put this code in any other Activity