chntgomez
chntgomez

Reputation: 2119

Keep Activity "alive" when calling "StartActivityForResult"

I have been trying everything to start an Activity for Result but keeping the caller activity seems impossible.

I Have tried setting on the caller activity in the manifest "android:onConfigChanges" for screenSize, orientation and keyboardHidden.

It is very important because in the caller Activity an Asyn Task loads the content from a Web Service.

When the Activity gets resumed after taking the picture, the Caller activity gets destroyed and loads again the entire UI from the Web Service and destroy the user imputs. Is there a way to prevent this?

This is the Caller method for the camera intent.

private void openCamera(){
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            //takePictureIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); <- Commented and not commented with the same problem.
            // Create the File where the photo should go
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(photoFile));
                startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
            }
        }
    }

An this is the "createImageFile()":

private File createImageFile() throws IOException {
    // Create an image file name
    File file;
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = timeStamp + "_";
    File storageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), "myAppDirectory");
    if (!storageDir.mkdir()){
        storageDir.mkdirs();
    };

    file = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    mCurrentPhotoPath = file.getAbsolutePath();
    return file;
}

Upvotes: 0

Views: 1216

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007533

I have been trying everything to start an Activity for Result but keeping the caller activity seems impossible.

Most likely, your process is being terminated due to low memory conditions. This is fairly typical when using ACTION_IMAGE_CAPTURE or other actions that launch a third-party camera app. It also happens in many other places as well, as your app's process will not live forever.

It is very important because in the caller Activity an Asyn Task loads the content from a Web Service.

Please bear in mind that there are plenty of other cases when your process will be terminated, and you need to be able to deal with this. So, if your concern is re-doing the Web service call, cache the results of the previous call in a file, and only re-issue the call if your cached results are stale. You might also consider whether your activity should be doing this Web service call at all, moving that logic instead into an IntentService.

Upvotes: 2

Related Questions