我的onStart()事件如下所示:
protected void onStart() {
super.onStart();
ShowProgressDialog();
Function1(); //this takes a lot of time to compute
HideProgressDialog();
Function2(); //this function uses the values calculated from Function1
}但是ProgressDialog不会显示。
PS:对于我的问题,AsyncTask不是一个好的解决方案,因为Function2需要从Function1计算出的值,而我真的不想链接4-5个AsyncTasks。
发布于 2014-03-11 19:20:35
在onstart上编写以下代码
pDialog = new ProgressDialog(this);
pDialog.setMax(5);
pDialog.setMessage("Loading...");
pDialog.setCancelable(true);
pDialog.show();发布于 2014-03-11 19:24:18
看起来activity还不在activity堆栈的顶部。可以在文档中读到:http://developer.android.com/reference/android/app/Activity.html
此外,如果您确实有处理,它可能会阻塞UI线程。我建议把它放在A-Sync任务中。在异步任务中,顺序仍然是从上到下,所以不需要创建多个a-synctask。
发布于 2014-03-11 19:27:53
您需要在后台线程中运行Function1和Function2。只有在所有OnStart()都完成之后,ProgressDialog才会显示出来。因此,您需要线程化耗时的代码来释放UI,否则进度对话框将不会显示。这在Android中总是很好的做法,如果你在主线程上运行耗时的东西,操作系统会用应用程序没有响应的消息来纠缠用户。一些伪代码:
OnStart()
{
ShowProgressDialog();
StartTimeConsumingThread();
}然后,在耗时的线程中:
TimeConsumingThread()
{
Function1();
Function2();
RunOnUiThread(
CloseProogressDialog();
)
}https://stackoverflow.com/questions/22324112
复制相似问题