单击onBackPressed

时间:2017-05-17 09:49:56

标签: java android

我想在Start方法中调用相机意图并且它工作正常,但是当我点击后退按钮时它不会从相机返回,因为Uri是空的。我想点击后退按钮转到上一个活动。

 @Override
protected void onStart() {
    super.onStart();
  if (uri==null){
      Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      startActivityForResult(takePictureIntent, CAMERA_REQUEST_CODE);
   }
}
@Override
public void onBackPressed() {
    super.onBackPressed();

}

1 个答案:

答案 0 :(得分:0)

创建一个带有静态布尔变量的辅助类,如下所示,

public class HelperUtil
    {
        public static boolean isCameraActivityBackPressed;
    }

稍微改变你的代码,

    @Override
        protected void onStart()
        {
            super.onStart();
            if (uri == null)
            {
                if (HelperUtil.isCameraActivityBackPressed)
                {
                    // this block will execute when ActivityCamera back is pressed and uri is null

                    startActivity(new Intent(this, ReplaceWithYourActivity.class));

                } else
                {
                    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(takePictureIntent, CAMERA_REQUEST_CODE);
                }
            }
        }

        @Override
        public void onBackPressed()
        {
            super.onBackPressed();

            HelperUtil.isCameraActivityBackPressed = true;

            Toast.makeText(ActivityCamera.this, "onbackpressed", Toast.LENGTH_LONG).show();
            startActivity(new Intent(this, ActivityCamera.class));
            finish();
        }