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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With