Search code examples
androidsamsung-mobile

back key issue when camera started in samsung


    final Dialog dailog = new Dialog(getActivity());
    LayoutInflater inflater = LayoutInflater.from(getActivity());

    dailog.setOnKeyListener(new OnKeyListener() {

    @Override
    public boolean onKey(DialogInterface dialog,int keyCode, KeyEvent event) {

            if (keyCode == KeyEvent.KEYCODE_BACK) {
                      getActivity().finish();//Line-9
            } 
    }
    });

    View dlgView = inflater.inflate(R.layout.xyz, null);
    dailog.setContentView(dlgView);
    ((Button) dlgView.findViewById(R.id.button_camera)).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
            startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
            dialog.dismiss();
        }
    });
    dialog.show();

When camera get started and back key is pressed,Line-9 get also executed in some devices(like samsung s3). Is there any solution to solve this problem?


Solution

  • when you start camera set boolean isCamaraOn = true;

    and when camera is on and you press back button

    then

     if(!isCameraOn){//go in only if camera is off
           if (keyCode == KeyEvent.KEYCODE_BACK) {
                      getActivity().finish();//Line-9
            }
     }
    

    here you can set isCamaraOn to false;

    try this, hope works for you