Dylan Leggio
Dylan Leggio

Reputation: 21

Android Development - new activity opening too quickly

I'm working on an app that takes a picture and then switches to a new activity. Here is the function that does so:

public void takePhoto(View view){
    final int REQUEST_IMAGE_CAPTURE = 1;

    // open camera
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if(takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }

    // open "Enter Info" screen
    startActivity(new Intent(MainActivity.this, EnterInfoActivity.class));
}

However, the "Enter Info" activity opens before the camera. I would like the camera to open before starting the new activity. Pressing "back" brings me to the camera, which to me means that the camera does indeed load, but the activity is given a higher priority.

Upvotes: 2

Views: 107

Answers (3)

Alex Ruiz
Alex Ruiz

Reputation: 415

As I see, you are using startActivityForResult to start the photo activity, which means that this new Activity you are opening will return a value calling the method onActivityResult.

Implement the method, and check if you come from the activity of the photo, then open the next activity.

Your flow would be something like that:

  • Start photo activity
  • When the photo activity ends, the method onActivityResult will be called
  • From onActivityResult start the EditInfo activity

You can find specific information about managing activities from this answer: How to manage `startActivityForResult` on Android?

PD: I can not test code and there has been some time since I wrote code for Android. If I get home and I can start a little android project without hassle I'll post some example.

Upvotes: 1

Lovekush Vishwakarma
Lovekush Vishwakarma

Reputation: 3179

private void captureImage() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

    // start the image capture Intent
    startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}

in onActivityResult Open Your Second Activity

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // if the result is capturing Image
    if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // successfully captured the image
            // display it in image view
            previewCapturedImage();
        } else if (resultCode == RESULT_CANCELED) {
            // user cancelled Image capture
            Toast.makeText(getApplicationContext(),
                    "User cancelled image capture", Toast.LENGTH_SHORT)
                    .show();
        } else {
            // failed to capture image
            Toast.makeText(getApplicationContext(),
                    "Sorry! Failed to capture image", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}

moew info see this, http://www.androidhive.info/2013/09/android-working-with-camera-api/

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1007474

I would like the camera to open before starting the new activity.

Then do not call startActivity() for EnterInfoActivity until you get called in onActivityResult() from the ACTION_IMAGE_CAPTURE activity. So, you wait for the results from ACTION_IMAGE_CAPTURE, then start the next activity.

Upvotes: 0

Related Questions