How to display ProgressDialog when preparing to show another activity?
Asked Answered
M

1

1

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.

Meryl answered 3/7, 2011 at 14:40 Comment(0)
I
1

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.

Iminourea answered 3/7, 2011 at 14:46 Comment(2)
thanks for your reply. Actually if I do nothing at onCreate(), but just show the map - still it hangs for some time. Looks like loading of map activity itself takes much time. Should I try to start that activity in ASyncTask?Meryl
OK, usage of ASyncTask helped.Meryl

© 2022 - 2024 — McMap. All rights reserved.