Prasad
Prasad

Reputation: 1375

How to implement Back button functionality in android

I want to implement back button functionality without pressing any hard keys. For example using proximity sensor. when any object comes to near to proximity I want to get previous activity. How to implement this in android is it possible ?. my code is like this :

SensorEventListener accelerometerSensorEventListener
        = new SensorEventListener(){

            public void onAccuracyChanged(Sensor sensor, int accuracy) {
                // TODO Auto-generated method stub

            }

            public void onSensorChanged(SensorEvent event) {
                // TODO Auto-generated method stub

                if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){              

                     finish();                  
                }

            }

     };

Thanks in advance

Upvotes: 0

Views: 2358

Answers (4)

Sanket
Sanket

Reputation: 499

You can use

//On Back Pressed 

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);
}strong text

Upvotes: 2

Ronak Mehta
Ronak Mehta

Reputation: 5979

You can use

@Override
public void onBackPressed() 
{
    Log.d("CDA", "onBackPressed Called"); 
}

or

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

Upvotes: 1

biegleux
biegleux

Reputation: 13247

You can call onBackPressed().

Upvotes: 0

guness
guness

Reputation: 6616

inside the listener of the sensor, you may finish() the current activity. so it goes back to the caller activity.

Upvotes: 0

Related Questions