Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

android - Show a progress bar when an Activity is loading

I have a ListActivity which launches another Activity based on the list selection. This second Activity needs to load a fair bit of data from the internet and as such there is a noticeable delay between when the user clicks on an item and when the Activity displays.

This is a problem because I currently have no way to indicate to the user that their click is being processed (even just changing the colour of the selected list item would be sufficient but I can't find a good way to do that). Ideally I'd be able to display an indeterminate ProgressDialog while the second Activity is loading.

I've tried a few different approaches for this but nothing seems to work as desired.


I've tried the following:

  • Retrieving the serializable data (not all of it but some part) in an AsyncTask in the first Activity and passing it as an extra to the second. This didn't really work well as a ProgressDialog I created in onPreExecute() didn't display immediately (it seems delayed by the processing done in doInBackground() for some reason.)

Here is the code for that:

AsyncTask<String, Void, String> read = new AsyncTask<String, Void, String>() {
    Dialog progress;

    @Override
    protected void onPreExecute() {
        progress = ProgressDialog.show(SearchActivity.this, 
                "Loading data", "Please wait...");
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {
        DatasetReader reader = new DatasetReader();
        reader.setFundID(params[0]);
        reader.addDatsets(FundProfile.datasets);
        reader.populate();
        return reader.toString();
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        progress.dismiss();
    }
};
read.execute(selectedItem.getUniqueID());
try {
    action = new Intent(SearchActivity.this, FundProfile.class);
    action.putExtra("data", read.get());
} catch(Exception ex) {
    ex.printStackTrace();
}
  • In the second Activity's onCreate() method (this does not work at all):

    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setProgressBarVisibility(true);

Here is the onCreate() method for the second approach:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setTitleColor(Color.WHITE);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setProgressBarVisibility(true);

    try {
        setContentView(R.layout.fund_profile);
        // init some data
        setProgressBarVisibility(false);
    } catch(Exception ex) {
        FundProfile.this.finish();
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...