krsna
krsna

Reputation: 4333

How do I go back to previous screen on pressing back button in Android

When Clicked on a button on screen, Camera will be opened. I saw few posts on stack overflow regarding this question. I tried finish(), finishActivity(), onBackPressed() methods after dispatchTakePictureIntent(); in onClick method but then failed! It exits my entire app.

This are the excerpts from my full code:

Button's onClick method:

 mCaptureImage.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          Log.d("TAG", "Camera opened");
          dispatchTakePictureIntent();
      }
  });

This method dispatches Intent:

private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                // Error occurred while creating the File
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                photoURI = FileProvider.getUriForFile(this,
                        "com.pc.android.fileprovider",
                        photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            }
        }
    }

My Question is, How do I go back to the previous screen when the back button is pressed when I'm on Camera's Intent.

Upvotes: 0

Views: 2038

Answers (2)

Raut Darpan
Raut Darpan

Reputation: 1530

Thus you have to do Nothing :

you cant override methods in external activities you are calling. However, when a user hits back in an activity that was called using startActivityForResult the response code RESULT_CANCELLED is generally returned (there may be instances where this isn't true). In your onActivityResult method simply check for the RESULT_CANCELLED code and call whatever functionality you need

Upvotes: 2

Aditi
Aditi

Reputation: 389

Could you try using startActivityForResult and check if your onActivityResult gets called or on back press Activity's onResume gets called

Upvotes: 0

Related Questions