Raúl Omaña
Raúl Omaña

Reputation: 921

Wait an asynchronous operation complete android

I'm begining with Android development. I have an asynchronous operation, but my intention is wait the operation is completed, and after that, continue the execution of my program. I have used AsyncTask, and all its methods, but I got error, because on the onPreExecute method I want to show another Activity, so I guess I can't show another Activity. That's the reason I want to wait to complete the asynchronous operation.

Greetings

1st edit: I've used AsyncTask(onPreExecute, doInBa..., onPost... ), but none method works. I understand how it works the AsyncTask class, but I want stop the execution when I invoke one asynchronous thirdparty method, because in the listener that needs, I change the value of a String variable X, and after invoke my method, that uses the thirdparty method, I use the variable X. I got an exeption because the variable hasn't updated.

Upvotes: 1

Views: 1563

Answers (2)

Nick
Nick

Reputation: 9373

Not entirley sure what the question is but AsynTask has a method that you can call after everything has completed. A good practice is to have a dialog or a spinner showing that work is happening then dismiss it.

    class loadingTask extends AsyncTask<String, Void, String> {

        protected String doInBackground(String... urls) {
            //Do work

        }

        protected void onPostExecute(String s) {
            ShowProgress.dismiss();

            Intent myIntent = new Intent(this, PostExAct.class);
            startActivity(myIntent);

        }
    }

In your on create:

ShowProgress = ProgressDialog.show(MainActivity.this, "",
            "Loading. Please wait...", true);
    new loadingTask().execute("params here");

This will show a loading dialog and while the work is being done then will be dismissed when the work is finished.

Upvotes: 0

Volodymyr Lykhonis
Volodymyr Lykhonis

Reputation: 2976

Please, read and follow example of AsynTask. You need to override onPostExecute

Upvotes: 1

Related Questions