Skip to content Skip to sidebar Skip to footer

Why When I Call An Asynctask From The Activity Onresume Do I Not Get To See The Progressdialog?

When a user presses a tab in my application - I want a background task to get kicked off which shows a progress bar. I do this by launching a AsyncTask from the onResume() of my ac

Solution 1:

Try this....

Use the ProgressDiaglog initialization before executing the execute() method of AsyncTask<> from UI thread, and call dismiss() in the doInBackground() method of AsyncTask<> before the return statement...

An example to explain it better....

publicclassAsyncTaskExampleActivityextendsActivity 
{
        protectedTextView _percentField;
        protectedButton _cancelButton;
        protectedInitTask _initTask;
        ProgressDialog pd;

    @OverridepublicvoidonCreate( Bundle savedInstanceState ) 
    {
        super.onCreate(savedInstanceState);

        setContentView( R.layout.main );

        _percentField = ( TextView ) findViewById( R.id.percent_field );
        _cancelButton = ( Button ) findViewById( R.id.cancel_button );
        _cancelButton.setOnClickListener( newCancelButtonListener() );

        _initTask = newInitTask();


         pd = ProgressDialog.show(AsyncTaskExampleActivity.this, "Loading", "Please Wait");


        _initTask.execute( this );
    }

    protectedclassCancelButtonListenerimplementsView.OnClickListener 
    {
                publicvoidonClick(View v) {
                        _initTask.cancel(true);
                }
    }

    /**
     * sub-class of AsyncTask
     */protectedclassInitTaskextendsAsyncTask<Context, Integer, String>
    {
        // -- run intensive processes here// -- notice that the datatype of the first param in the class definition matches the param passed to this method // -- and that the datatype of the last param in the class definition matches the return type of this method@OverrideprotectedStringdoInBackground( Context... params ) 
                {
                        //-- on every iteration//-- runs a while loop that causes the thread to sleep for 50 milliseconds //-- publishes the progress - calls the onProgressUpdate handler defined below//-- and increments the counter variable i by one
                        int i = 0;
                        while( i <= 50 ) 
                        {
                                try{
                                        Thread.sleep( 50 );
                                        publishProgress( i );
                                        i++;
                                } catch( Exception e ){
                                        Log.i("makemachine", e.getMessage() );
                                }
                        }
                        pd.dismiss(); 
                        return"COMPLETE!";
                }

                // -- gets called just before thread begins@OverrideprotectedvoidonPreExecute() 
                {
                        Log.i( "makemachine", "onPreExecute()" );

                        super.onPreExecute();

                }

                // -- called from the publish progress // -- notice that the datatype of the second param gets passed to this method@OverrideprotectedvoidonProgressUpdate(Integer... values) 
                {
                        super.onProgressUpdate(values);
                        Log.i( "makemachine", "onProgressUpdate(): " +  String.valueOf( values[0] ) );
                        _percentField.setText( ( values[0] * 2 ) + "%");
                        _percentField.setTextSize( values[0] );
                }

                // -- called if the cancel button is pressed@OverrideprotectedvoidonCancelled()
                {
                        super.onCancelled();
                        Log.i( "makemachine", "onCancelled()" );
                        _percentField.setText( "Cancelled!" );
                        _percentField.setTextColor( 0xFFFF0000 );
                }

                // -- called as soon as doInBackground method completes// -- notice that the third param gets passed to this method@OverrideprotectedvoidonPostExecute(String result ) 
                {

                        super.onPostExecute(result);
                        Log.i( "makemachine", "onPostExecute(): " + result );
                        _percentField.setText( result );
                        _percentField.setTextColor( 0xFF69adea );
                        _cancelButton.setVisibility( View.INVISIBLE );
                }
    }    
}

Solution 2:

OK! Eventually via the android-dev IRC channel the solution was found.

The reason the task was working but the progressPublish calls were not filtering back to the UI was because i was calling the AsyncTask execute, then immediately calling the .get() method - which blocks - tieing up the UI thread whilst the asynctask was trying to use it!

So I removed the .get() method, and put the relevant code sections into the onPostExecute - and all is working perfectly!

Post a Comment for "Why When I Call An Asynctask From The Activity Onresume Do I Not Get To See The Progressdialog?"