准备显示另一个活动时如何显示ProgressDialog?
如果用户长时间单击列表项,我需要使用 MapView 显示活动。 这个过程需要一段时间,所以我想在应用程序挂起时显示用户进度对话框。 这是代码:
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setOnItemLongClickListener (new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView parent, View view, int position, long id) {
...
ProgressDialog dialog = ProgressDialog.show(getApplicationContext(), "", "Loading. Please wait...", true);
Intent intent = new Intent(getBaseContext(), Map.class);
startActivity(intent);
我选择了正确的方法吗?现在获取不同的 FC(取决于为 ProgressDialog 选择的上下文)。 ProgressBar 可以在我的场景中显示吗?
更新。我尝试在开始活动之前显示 Toast。同样,只有当地图已经显示时才会显示 Toast。不明白发生了什么。如果我删除 startActivity 代码,则会立即显示 Toast。
I need to show activity with MapView, if user long clicked on the list item.
This process takes a while, so I would like to show user progressdialog, while application hangs.
Here is the code:
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setOnItemLongClickListener (new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView parent, View view, int position, long id) {
...
ProgressDialog dialog = ProgressDialog.show(getApplicationContext(), "", "Loading. Please wait...", true);
Intent intent = new Intent(getBaseContext(), Map.class);
startActivity(intent);
Have I chosen correct approach? Getting different FCs now (depending on the context chosen for ProgressDialog). Can ProgressBar be shown in my scenario?
Upd. I've tried to show Toast before starting activity. Again, Toast is shown only when Map is already displayed. Don't understand what happens. If I remove startActivity code, then Toast is displayed immediately.
您是否在 MapView 的 onCreate() 中进行了冗长的准备工作?你不应该这样做,因为它会阻塞 UI 线程......
相反,你应该做的 - 在 Map 活动的 onCreate() 内部,你应该生成一个新的 AsyncTask (理想情况下)并在那里显示进度条(并退出 onCreate()显示进度条后立即)。然后在 AsyncTask 完成后(在 postExecuted() 中),您应该关闭进度对话框并显示您的地图。 PostExecuted() 在 UI 线程中运行,因此您可以安全地关闭进度条。
您拥有的 FC 以及可能的 ANR(未响应)都可能会出现,因为您在 UI 线程中/外执行了某些操作。你应该在 UI 线程中创建/取消你的 UI 组件,并且你不应该在 UI 线程中运行冗长的操作。这是经验法则。
Are you doing the lenghty preparation itself in MapView's onCreate() ? You should not because it will block the UI thread....
Instead what you should do - inside of the Map activity's onCreate(), you should spawn a new AsyncTask (ideally) and show progress bar there (and exit the onCreate() right after showing progress bar). Then in the AsyncTask after it finishes (in postExecuted()) you should dismiss the progress dialog and show your map. PostExecuted() is run in UI thread so you can safely dismiss the progress bar.
FCs you have and possibly ANRs (Not responding) are all probably coming because you do certain things in/out of the UI thread. You SHOULD create/dismiss your UI components in UI thread, and you SHOUDL NOT run lenghty operation in the UI thread. That's the rule of thumb.