0

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

4 Answers 4

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

2 Comments

Yes I tried it but it is not working. I will edit and post my code plz check it
can you try this: Intent i = new Intent(CurrentActivity.this, CallerActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(i);
0

You can call onBackPressed().

1 Comment

Have you registered your listener?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.