Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show Progress bar while switching between activities

I have two activities Activity1 and Activity2. When clicked on button, I am switching from Activity1 to Activity2. But Activity2 is taking a lot of time to load due to slow internet speed. Empty screen is shown till the activity is loaded.

Instead of the black screen, I want to show a progress bar and when the Activity2 is ready, then close progress bar without making the user to get frustrated.

I don't have any idea of how to do this or start this. I am new to android. Please help me by suggesting idea or please share any links regarding this!!

Thanks in advance!!

like image 568
Mahe Avatar asked Nov 26 '25 04:11

Mahe


1 Answers

you can use an AsyncTask in the onCreate of activity 2. in onPreExecute show the progressdialog. In onDoinBackground complete the download. when done in onPostExecute hide the progressdialog and show the content in UI.

For Example Code

private class YourTask extends AsyncTask<Void, Void, ContentType> {
    protected void onPreExecute() {
        //show progressdialog here
    }

         protected ContentType doInBackground() {
             // download content
             return content;
         }


         protected void onPostExecute(ContentType content) {
             // hide progress dialog
              // show the content
         }
     }
like image 180
stinepike Avatar answered Nov 27 '25 20:11

stinepike



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!