为什么当我调用AsyncTask的从活动的onResume我没有能看到progressdialog? [英] Why when I call an AsyncTask from the activity onResume do I not get to see the progressdialog?
问题描述
当用户presses在我的应用程序选项卡 - 我希望有一个后台任务获得拉开序幕,显示一个进度条
When a user presses a tab in my application - I want a background task to get kicked off which shows a progress bar.
我做到这一点,我发起活动,从onResume一个AsyncTask的()。的问题是,这样做时,我的进展对话未示出 - 后台任务成功运行和onPostExecute运行后焦点返回到我的活动和应用程序继续正常
I do this by launching a AsyncTask from the onResume() of my activity. The problem is that my progress dialog is not shown when doing this - the background task runs successfully and after the onPostExecute is run focus is returned to my activity and the application continues as normal.
我怎么能启动的AsyncTask从onResume =或当活动开始仍显示该活动的布局/进度条上的preExecute创造的?
How can I launch a AsyncTask from the onResume = or when an activity is started with still show the activity's layout / progress bar created on onPreExecute?
目前的code行为如下:
Currently the code acts as follows:
http://pastebin.com/KUsg3Mri 然而,当我改变选项卡,然后的onCreate被调用时,asyntask是拉开序幕 - 而没有改变标签的内容,或显示进度条,当AsyncTask的结束 - 的活动则显示其所有的GUI成功要素
http://pastebin.com/KUsg3Mri however when i change tab and onCreate is called, the asyntask is kicked off - without ever changing the tab's contents, or showing the progress bar, when the asynctask finishes - the activity then shows all its gui elements successfully
看来,如果我这样做: http://pastebin.com/KUsg3Mri 并揭开序幕findReplays无论从的onCreate或onResume - 这将显示进度对话框 - 但publishProgress永远不会调用onProgressUpdate - 所以它更接近,但没有雪茄!
It seems as if i do this : http://pastebin.com/KUsg3Mri and kick off the findReplays from either onCreate or onResume - this will show the progress dialog - but the publishProgress never calls onProgressUpdate - so its closer, but no cigar!
推荐答案
试试这个......
Try this....
执行的AsyncTask&LT的execute()方法之前使用 ProgressDiaglog初始化;从UI线程> 和调用解雇()中的AsyncTask 1所述的doInBackground()方法;>返回前声明...
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...
一个例子来解释它更好的......
public class AsyncTaskExampleActivity extends Activity
{
protected TextView _percentField;
protected Button _cancelButton;
protected InitTask _initTask;
ProgressDialog pd;
@Override
public void onCreate( 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( new CancelButtonListener() );
_initTask = new InitTask();
pd = ProgressDialog.show(AsyncTaskExampleActivity.this, "Loading", "Please Wait");
_initTask.execute( this );
}
protected class CancelButtonListener implements View.OnClickListener
{
public void onClick(View v) {
_initTask.cancel(true);
}
}
/**
* sub-class of AsyncTask
*/
protected class InitTask extends AsyncTask<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
@Override
protected String doInBackground( 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
@Override
protected void onPreExecute()
{
Log.i( "makemachine", "onPreExecute()" );
super.onPreExecute();
}
// -- called from the publish progress
// -- notice that the datatype of the second param gets passed to this method
@Override
protected void onProgressUpdate(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
@Override
protected void onCancelled()
{
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
@Override
protected void onPostExecute( String result )
{
super.onPostExecute(result);
Log.i( "makemachine", "onPostExecute(): " + result );
_percentField.setText( result );
_percentField.setTextColor( 0xFF69adea );
_cancelButton.setVisibility( View.INVISIBLE );
}
}
}
这篇关于为什么当我调用AsyncTask的从活动的onResume我没有能看到progressdialog?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!