Why onActivityResult() called before uploading to Flickr finishes using Flickrj-android

109 views Asked by At

I need to do a feature like this for my app: when user presses a "Save" button, the selected image will be uploaded to Flickr, and then getting the source of uploaded image from Flickr to store into database. I use flickrj-android library to do. This is my code:

//call this function when "Save" button is clicked
Intent intent = new Intent(getApplicationContext(),FlickrjActivity.class);
intent.putExtra("flickImagePath", currentScreenshot);   
startActivityForResult(intent, UPLOAD_FLICKR);
Log.i("ordering","afterStartFlickrUpload");

onActivityResult()

switch(requestCode){
   case UPLOAD_FLICKR:
         Log.i("ordering","beforeResultCode");
         if(resultCode == RESULT_OK){   
             String flickr_source = data.getStringExtra("flickr_source");
             Log.i("flickr",flickr_source);
         }
         break;
}

And I have an Activity called FlickrjActivity which creates an AsyncTask called UploadPhotoTask UploadPhotoTask has a method called onPostExecute which has a parameter which is the Flickr photo ID of uploaded photo as a response from Flickr API

protected void onPostExecute(String response) {
   Log.i("photoID",response);
}

This is load() method of the FlickrjActivity:

private void load(OAuth oauth) {
    if (oauth != null) {

        UploadPhotoTask taskUpload = new UploadPhotoTask(this, new File(
                path));
        taskUpload.setOnUploadDone(new UploadPhotoTask.onUploadDone() {

            @Override
            public void onComplete() {
                Intent intent = new Intent();
                intent.putExtra("flickr_source", "This is a link");
                setResult(RESULT_OK, intent);
                finish();
            }
        });

        taskUpload.execute(oauth);
    }
}

My expected process is the main activity creates an intent which calls FlickrjActivity, which will create UploadPhotoTask to upload the photo to Flickr, get the photoID and then returns this ID to main activity using setResult(RESULT_OK,intent); and finish();

But right after I press the Save button, the onActivityResult() called since Log.i("ordering","beforeResultCode"); prints message to Logcat before uploading runs, so the block inside if(resultCode == RESULT_OK) never been called since onActivityResult() won't be called again after uploading completes. So why onActivityResult() be called before finish() of FlickrjActivity?

1

There are 1 answers

0
keduadoi On

I did find the answer, just need to change android:launchMode from "singleTask" to "singleTop", although I don't know why.