简体   繁体   中英

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

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 ?

我确实找到了答案,只是需要将android:launchMode从“ singleTask”更改为“ singleTop”,尽管我不知道为什么。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM