5

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.

SuReSh PaTi
  • 1,373
  • 7
  • 28
  • 46

5 Answers5

12

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);
}
Stefano Ortisi
  • 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
2

just call the foolowing function this will close current activity and move you to the previous screen

finish();
coading fever
  • 221
  • 2
  • 10
0

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.

MattC
  • 12,285
  • 10
  • 54
  • 78
0
public void onBackPressed() {

        Intent intent = new Intent(CurrentActivity.this,
                PreviousActivity.class);
        startActivity(intent);

            return;
        }
Assad
  • 291
  • 1
  • 4
  • 16
Dhaval
  • 1,597
  • 11
  • 15
0

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.

Narendra
  • 609
  • 1
  • 12
  • 28