简体   繁体   中英

Perform remaining task of AsyncTask that was started in previous instance of the same activity

I am using AsyncTask to post some status on social media.

private class PostOnSocialMedia extends AsyncTask<String, Void, String> {

    @Override
    protected void onPreExecute() {

        pDialog.setTitle(R.string.posting_status_on_social_media);
        Log.d(TAG, "onPreExecuteCalled.");
    }

    @Override
    protected String doInBackground(String... params) {

        Log.d(TAG, "doInBackground.");

        currentActivity.this.twitter.tweet(params[0]);
        currentActivity.this.facebook.post(params[0]);
        currentActivity.this.googlePlus.share(params[0]);

        return null;
    }

    @Override
    protected void onPostExecute(String result) {

        if (pDialog.isShowing()) {
            pDialog.dismiss();
        }

        // Start next Activity when posted on all social media.
        Intent intent = new Intent(currentActivity.this,
                nextActivity.class);
        intent.putExtra("userMatches", userMatches);
        startActivity(intent);
        Log.d(TAG, "onPostExecute.");

    }
}

When user is not logged in to, lets say, Twitter I have to start a new Intent to show twitter page(so that user can log in). This causes the flow to return back to AsyncTask and calls a onPostExecute() method. And new Activity is started.

But I don't want this. What I want is that user login to Twitter and then it will come back to application (when user clicks login flow comes back to current activity. But the activity is created as a new one not the one from which I started a call to social medial post). When it comes back to application, I call a method of Twitter class to proceed to complete the uncompleted posting task. And once it is done, then only I want to go to next activity.

So, is there anyway to handle this situation?

EDIT :
Is it possible to solve this by using onSaveInstanceState(Bundle savedInstanceState) and onRestoreInstanceState(Bundle savedInstanceState) ?

在调用asynctask之前,请确保您的用户在两个平台上均已登录,然后再调用asynctask,因为现在在这种情况下,用户可以进行多种组合,所以我的建议是首先确保用户在两个facbook上均已登录,tiwtter等等,然后调用此asynctask,这样就不必让asynctask等待

AsyncTask used for background Process.

So you have to check All the condition before calling asynctask and then used.

Generally, AsyncTasks are to do stuff in background without blocking the UI-Thread. If you want your Main-Thread (= UI-Thread) to wait for your AsyncTask, you could just do it without an AsyncTask.

If you want to wait for it though, try yourTask.get(); , that should make your Main-Thread wait for the result of your Task. Look here for more info about get().

I solved my problem by using a static variable which is set to true just before recreating an Activity and when Activity execution is in onCreate I use this variable to decide whether this activity is recreated and if yes, then call method to perform remaining task.

When posting is done I call a method of activity, which calls a method to post on social media, from Twitter class where post is successfully done.

Method in activity :

public static void postDone() {
    // Go to next activity
}

Call from Twitter class :

MyActivity.postDone();

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