简体   繁体   中英

Display “Loading : Progress Bar ” in Android Intent till Data Load

I am working on an android app, in that app i have intent2 which on click redirects to intent3 and takes some time then loads a table and displays server data into it.

Sometimes if there is a lot of data, it tales pretty much time to get the dataload and the time blank screen is displayed increases.

i wish to show a loading bar till the data loads.

how can i show the ProgrssBar till only when data is not displayed ?

Probably your best bet would be to use AsyncTask in your "intent3":

You could do it like this:

private class performBackgroundTask extends AsyncTask <Void, Void, Void>  
      {
               private ProgressDialog Dialog = new ProgressDialog(ClassName.this);

               protected void onPreExecute()
               {
                   Dialog.setMessage("Please wait...");
                   Dialog.show();
               }

               protected void onPostExecute(Void unused)    
               {
                   try
                   {
                       if(Dialog.isShowing())
                       {
                           Dialog.dismiss();
                       }
                               // do your Display and data setting operation here
                   }
                   catch(Exception e)
                   {

                   }

            @Override
        protected Void doInBackground(Void... params) 
            {
           // Do your background data fetching here 
               return null;   
        }
      }

You probably need to run an AsyncTask on onCreate when you open the new activity, the structure of the asynctask would be like this (taken from the google doc ), notice that if you want to increament a progress bar you have to implement onProgressUpdate and call publishProgress in the doInBackground method

private class DownloadFilesTask extends AsyncTask<Void, Integer, Void> {

 protected void onPreExecute()
 {
     // show your progress bar   
 }

 protected Void doInBackground(Void... params) {
     // do your work and publish the progress
     publishProgress(progress);
 }

 protected void onProgressUpdate(Integer... progress) {
     setProgressPercent(progress[0]);
 }

 protected void onPostExecute(Void result) {
     //dismiss your progress bar
 }

}

This code is just an example, of course you need to adapt it to your logic/code.

Check out this simple and complete example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM