Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a default back key(on device) listener in android?

Tags:

I am having two activities A and B. when i click the button in A that will shows B. when i click the Button in B it backs to A. i had set the overridePendingTransition method after the finish() method. it works properly. but in case the current Activity is B. on that time i click the default back button in the device. it shows the right to left transition to show the Activity A.

How i can listen that Default back key on device?

EDIT:

Log.v(TAG, "back pressed");
finish();
overridePendingTransition(R.anim.slide_top_to_bottom, R.anim.hold);
like image 201
Praveen Avatar asked Apr 07 '10 11:04

Praveen


People also ask

What happens when back button is pressed in android?

However, when the back button is pressed, the activity is destroyed, meaning, the temporary data is lost during this call. This is what the official documentation states. But we do not want to lose this data. To retain the data, we need to override the back pressed method.

How can I tell if my android back button is pressed?

In order to check when the 'BACK' button is pressed, use onBackPressed() method from the Android library. Next, perform a check to see if the 'BACK' button is pressed again within 2 seconds and will close the app if it is so.


1 Answers

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        // do something on back.
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

The following link is a detailed explanation on how to handle back key events, written by the Android developers themselves:

Using the back key

like image 152
Jamie Keeling Avatar answered Oct 13 '22 00:10

Jamie Keeling