Reputation: 2737
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!!
Upvotes: 4
Views: 8261
Reputation: 39538
Start your Activity2 with a loading screen (a indeterminate progressbar, or circle is fine) or even better: cached data, and put your heavy stuff on a Thread. At the end of the thread, remove the progressbar and display your data.
With this idea in mind, Activity2 will launch really quickly and you can display wathever you want during data loading
Thread will avoid getting a black screen.
public class Activity2 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new Thread(new Runnable() {
public void run(){
//All your heavy stuff here!!!
}
}).start();
}
}
Note: you may also prefer an AsyncTask, you can see here a great comparaison between differnt way to achieve what you want:
http://techtej.blogspot.be/2011/03/android-thread-constructspart-4.html
Upvotes: 0
Reputation: 54672
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
}
}
Upvotes: 1
Reputation: 53
ASyncTask
is what you're looking for. It shouldn't be hard to implement.
Upvotes: 0