Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 'Back' button action

Tags:

android

Can i assign the action of the default Android 'Back' button into another button? I mean without having to write the code, is there a predefined 'Back' method?

Thanks!

like image 512
Saku Avatar asked Jul 05 '12 10:07

Saku


2 Answers

onClick of Button add onBackPressed();

public void onClick(){

  onBackPressed();

}
like image 93
Samir Mangroliya Avatar answered Nov 03 '22 15:11

Samir Mangroliya


There are two ways for your purpose:

1st: Override the onBackPressed method in your Activty:

@Override
    public void onBackPressed()
    {
        super.onBackPressed();
    }


2nd: Override onKeyDown and look for KeyEvent.KEYCODE_BACK

public boolean onKeyDown(int keyCode, KeyEvent event){
        if (keyCode == KeyEvent.KEYCODE_BACK)
        {

        }
        return false;
    }


If you want to combine both (lets say you want the backAction on the menuButton, It'll look like this:

public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_MENU)
        {
                onBackPressed();
        }
        return false;
    }
like image 38
Thkru Avatar answered Nov 03 '22 14:11

Thkru