Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function called during back button click on Android

Tags:

android

Which is the function called when you click the back button on android. My requirement is when an application is running and user clicks on the back button, the status of the application should be stored into the database and the user should be able to see the status whenever he returns back to the application.

Browsing through internet, I understood we can handle the control using onKeyDown() function. However even if I use it in my code, the function is not called when I click on back button. Below is the function:

  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
      Log.d(null,"In on Key Down");
      if (keyCode == KeyEvent.KEYCODE_BACK) {
          moveTaskToBack(true);
          return true;
      }
      return super.onKeyDown(keyCode, event);
  }

Please suggest, if anybody has faced the same/similar scenario.

like image 577
tech_learner Avatar asked Nov 28 '22 03:11

tech_learner


1 Answers

the activity function called when you press the back button is onBackPressed.

void onBackPressed() Called when the activity has detected the user's press of the back key.

upvote if you came here for this and not specifically for saving state :P

like image 79
mobooya Avatar answered Dec 17 '22 18:12

mobooya