1

I am trying to implement a solution that I found HERE but after I hit capture, the back button doesn't finish the activity. Is there a way to call

finish();

when the back button is pressed within the function

private void capture() {
    mCamera.takePicture(null, null, null, new Camera.PictureCallback() {
        final float bearing = degree;

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            latitude = mGpsLocationTracker.getLatitude();
            longitude = mGpsLocationTracker.getLongitude();

            Toast.makeText(getApplicationContext(), "Picture Taken",
                    Toast.LENGTH_SHORT).show();

            Toast.makeText(getApplicationContext(), "" +
                    latitude + ", " + longitude, Toast.LENGTH_SHORT).show();

            Toast.makeText(getApplicationContext(), "" + bearing, Toast.LENGTH_SHORT).show();

            Intent intent = new Intent();
            intent.putExtra("image_arr", data);
            setResult(RESULT_OK, intent);
            camera.stopPreview();
            if (camera != null) {
                camera.release();
                mCamera = null;
            }
            finish();
        }
    });
}

@Override
public void onBackPressed() {
    super.onBackPressed();  
    finish();
}
Community
  • 1
  • 1
Ryan Newman
  • 846
  • 3
  • 17
  • 35

2 Answers2

1

Ok instead of doying finish(); try to make an intent to the desired activity:

@Override
public void onBackPressed() {
    super.onBackPressed();
    startActivity(new Intent(YourActivity, DesiredActivity));
    finish();
}
Fakher
  • 2,098
  • 3
  • 29
  • 45
  • That worked! Do you happen to know why the regular back button wasn't working? I'm just curious – Ryan Newman Sep 15 '15 at 17:59
  • sorry i don't have an explanation about that, but may be because you must specifie an activity to go before you kill the current activity. – Fakher Sep 16 '15 at 07:55
0

Try Overriding onKey method in your activity class

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) 
{   if (event.getAction() == KeyEvent.ACTION_UP) 
    {   finish();
    }
    return false;
};
Angad Singh
  • 1,032
  • 1
  • 17
  • 36