简体   繁体   中英

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

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

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

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

您可以调用onBackPressed()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM