Search code examples
androidandroid-intentandroid-activitycordova-3

How can I call a parent activity from a third subactivity


I am currently adding a cordova plugin to launch my own custom camera application. I am also including Aviary to do some photo editing. Now my biggest problem is I don't know how to return from activity (3) back to activity (1) where I need to be able to access and call:

this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));

I have searched through many posts on stackoverflow and numerous blogs. I have followed the convection I have seen for returning to a parent activity, but I haven't seen anything where one goes from A -> B -> C -> A. My current code is posted below. Currently I am able to go up from Aviary -> to my second activity (the camera preview), but I am stumpted as to why I can't go up one level further.

Is it not possible to have to go up that far?
Is it not possible to just add finish(); to go up as far back as you want?

My execute method resides in Activity 1 according to the cordova documentation:

(1) CordovaPlugin

public class Aviary extends CordovaPlugin{

    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
            this.callbackContext = callbackContext;

            Intent i = new Intent(cordova.getActivity(), CameraActivity.class);
            this.cordova.startActivityForResult((CordovaPlugin) this, i, (CAMERA + 1) * 16 + returnType + 1);


    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        logger.i("ACTIVITYRESULT - returned from cameraActivity, now in aviary");

        if (resultCode == Activity.RESULT_OK && requestCode == CameraConstants.FROM_CAMERA_TO_EXECUTE)
            // I need this to launch with the uri data contained within the intent
            //build json array here
            this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));

My second activity then starts with a camera preview. Once the image is captured I launch an activity with the captured bitmap and display it on the screen with options to either post the image or edit it with aviary.

(2) CameraActivity: launching postcapture screen

Intent intent = new Intent(context, PostCaptureActivity.class);
intent.putExtra(CameraConstants.URI, path);
intent.putExtra("requestCode", CameraConstants.FROM_SAVEIMAGETASK_TO_POSTCAPTURE);
intent.putExtra(CameraConstants.SOURCE, CameraConstants.SOURCE_CAMERA);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivityForResult(intent, CameraConstants.FROM_SAVEIMAGETASK_TO_POSTCAPTURE);

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

    if (requestCode == CameraConstants.FROM_POSTCAPTURE_TO_PREVIEW)
    {
        logger.i("ACTIVITYTHREAD - inCameraActivity about to call finish");
        Intent intent= new Intent();
        setResult(RESULT_OK, intent);
        //intent.putExtra("uri", data.getData());
        intent.putExtra("requestCode", CameraConstants.FROM_CAMERA_TO_EXECUTE);
        finish();
    }

Activity 3: PostCaptureActivity (launching Aviary)

private void setupAviaryIntent() {
    logger.i("Launching Aviary");
    Intent intent = new Intent(PostCaptureActivity.this, FeatherActivity.class);
    intent.setData(Uri.parse(uri));
    intent.putExtra("requestCode", CameraConstants.LAUNCH_AVIARY);
    intent.putExtra( "output-quality", 100 );
    intent.putExtra( "output-format", Bitmap.CompressFormat.JPEG.name() );
    intent.putExtra( "effect-enable-fast-preview", true );
    intent.putExtra("tools-list", new String[]{"CROP", "EFFECTS", "ADJUST", "ENHANCE",
            "DRAWING", "TEXT", "SHARPNESS", "BRIGHTNESS", "CONTRAST",
            "BLEMISH", "SATURATION", "RED_EYE", "WHITEN", "COLORTEMP"});
    startActivityForResult(intent, CameraConstants.LAUNCH_AVIARY);
}


@Override
public void onActivityResult( int requestCode, int resultCode, Intent data )
{
    super.onActivityResult(requestCode, resultCode, data);
    /** FROM: Aviary TO: Preview*/
    if(resultCode == RESULT_OK && requestCode == CameraConstants.LAUNCH_AVIARY )
    {
        logger.i("in PostCaptureActivity, should go back to CameraActivity");
        Uri uri = data.getData();
        Intent intent = new Intent();
        setResult(RESULT_OK, intent);
        intent.putExtra("requestCode", CameraConstants.FROM_POSTCAPTURE_TO_PREVIEW);
        intent.putExtra("uri", uri);
        finish();
    }
}

What I am looking for is either a way to go from activity 3 and return back to the parent activity (1) or some example of how I could jump back to activity one and be able to run the sendPluginResult method with my picture information.

EDIT: So it turns out the reason I wasn't able to return to the previous activity after a finish(); was because the flag I added in one of the intents. After rereading the documentation I realized what the error was. After removing this line I was able to get everything working as intended.

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Solution

  • Use NO HISTORY flag in your second activity.