Minimize activity on back key press

Sandy picture Sandy · Jul 8, 2011 · Viewed 14.4k times · Source

OnBack Key press i want to minimize the application, How can i do this???

public boolean onKeyDown(int keyCode, KeyEvent event) {

   if (keyCode == KeyEvent.KEYCODE_BACK) {

           //Here i want to put minimize code.. pls give me this statement

       return true;
   }
   return super.onKeyDown(keyCode, event);

}

Thanks

Answer

Adil Soomro picture Adil Soomro · Jul 8, 2011
public boolean onKeyDown(int keyCode, KeyEvent event)  
    {
         if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
         {
            this.moveTaskToBack(true);
            return true;
         }
        return super.onKeyDown(keyCode, event);
    }

This will send the activity to the background. See documentation for further reference.