fred_dev
fred_dev

Reputation: 43

Cancel active intent from activity

I have this method that opens the native camera.

public void takePhoto(){
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    Uri photoUri;
    photoUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); 
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
    startActivityForResult(cameraIntent, CAM_REQUREST);

}

It is working great, but I want to be able to return to my activity if the user forgets to, or if there is something my app needs attention for. Is there a way that I can do this from my activity?

Upvotes: 0

Views: 157

Answers (2)

Mahi
Mahi

Reputation: 1784

Override onActivityResult in your activity

protected void onActivityResult(int requestCode, int resultCode,
        Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    Log.e(TAG, "--onActivityResult MainActivity--");


    if (requestCode == CAM_REQUREST && resultCode == RESULT_OK) {

        Uri selectedImage = intent.getData();
        Log.e(TAG, "Your image :" + selectedImage);



}


    }

Upvotes: 0

Kelevandos
Kelevandos

Reputation: 7082

startActivityForResult() will automatically return after the result is obtained, so after the photo is taken in this case. If you want to relaunch your app at any point, you should use startActivity() with your own package in the intent.

Do it like this:

PackageManager p = getPackageManager();
String myPackage = getApplicationContext().getPackageName();

Intent intent = p.getLaunchIntentForPackage(myPackage);
startActivity(intent);

Upvotes: 1

Related Questions