I have open pdf file through my application.when click on device back button it is automatically come back to my application .It is working fine.Here i want catch back button event in device.I override the back button.but it is not working.please help me.
-
you have open pdf file through Intent? – Mohsin Naeem Jun 29 '12 at 16:31
5 Answers
This is an example of what you are asking:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK ) {
//do your stuff
}
return super.onKeyDown(keyCode, event);
}

- 5,288
- 3
- 33
- 41
-
http://stackoverflow.com/questions/5312334/how-to-handle-back-button-in-activity. Check This – Dhaval Oct 21 '13 at 11:50
just call the foolowing function this will close current activity and move you to the previous screen
finish();

- 221
- 2
- 10
You would override the onKeyDown event in your Activity class and look for the keyCode of KEYCODE_BACK. To prevent further propagation you would return true
to stop the system from handling it and this should stop the back button from taking your user out of wherever you are.
This violates the rules of what a user expects to happen, though, and is not recommended unless you're overriding the back button for something like going back through pages in a WebView or something like that.

- 12,285
- 10
- 54
- 78
If you have handled pdf file related operations(like reading etc) in your application i.e. there is Activity or Fragment handling this pdf file operations in your application then you can overrided onBackPressed() method to put your logic there. But if you are opening pdf via intent i.e using other application using intents action parameter then you can not overrided there onBackPressed() or onKeyDown() event in your application. But instead of that you can put your logic inside overriding onActivityResult() method in your activity from which you are opening the pdf file.

- 609
- 1
- 12
- 28