Show a progress bar when an Activity is loading

前端 未结 3 666
灰色年华
灰色年华 2020-12-14 08:37

I have a ListActivity which launches another Activity based on the list selection. This second Activity needs to load a fair bit of d

相关标签:
3条回答
  • 2020-12-14 09:07

    Adam,

    It sounds like you are looking for the Indeterminate Progress bar: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ProgressBar2.html

    You can display this while you are loading your second Activity then set the visibility to false once the second Activity has loaded its data.

    0 讨论(0)
  • 2020-12-14 09:14

    If you have long operations you should not be doing them in onCreate in any case as this will freeze the UI (whether or not the activity is displayed). The UI set by onCreate will not appear and the UI will be unresponsive until after the onCreate call finishes.

    It seems you can start your second activity and display a progress bar (or requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);), then start an ASyncTask which will be responsible for updating your UI once data has been retrieved.

    0 讨论(0)
  • 2020-12-14 09:27

    Move creating the Intent -- and really anything you need to do after the AsyncTask completes -- into onPostExecute:

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        progress.dismiss();
        Intent action = new Intent(SearchActivity.this, FundProfile.class);
        action.putExtra("data", result);
        // ... do more here
    }
    

    The problem is that AsyncTask.get() blocks until the task is completed. So in the code above, the UI thread is blocked and the ProgressDialog is never given a chance to appear until the task completes.

    0 讨论(0)
提交回复
热议问题