Reputation: 10698
I recently discovered that since my app is pulling so much data from a few URLs, it takes about 3-7 seconds for it to load the main layout. So, I made a layout called 'loading' to display, which is just simply a TextView that states "Please wait while data is being collected...". However, when I run my app, it won't display the 'loading' layout. It simply goes black for a while, like it used to before, and then go to the main layout. I tried cleaning the project too, and it still does this. Here's a portion of my Main.java:
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.loading);
populateArray();
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Set up click listeners for all buttons
View v1 = findViewById(R.id.continueButton);
v1.setOnClickListener(this);
View v2 = findViewById(R.id.colorCheck);
v2.setOnClickListener(this);
View v3 = findViewById(R.id.terms);
v3.setOnClickListener(this);
}
populateArray();
is the method that is pulling all the information of the internet. So, I figured, "Why not tell it to set the content view immediately to 'loading', have it run populateArray();
, then display the main layout?" Obviously, I must be missing something here. Any ideas?
==========================================================================
EDIT: I tried using AsyncTask, but I'm getting a force close. I'm also getting a warning saying the AsyncTask class is never used. Here's my code:
P.S. ProgressDialog dialog;
is a global definition.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dialog = ProgressDialog.show(this, "",
"Please wait while data is collected...", true);
// Set up click listeners for all buttons
View v1 = findViewById(R.id.continueButton);
v1.setOnClickListener(this);
View v2 = findViewById(R.id.colorCheck);
v2.setOnClickListener(this);
View v3 = findViewById(R.id.terms);
v3.setOnClickListener(this);
}
And...
private class LoadData extends AsyncTask <String[][], String, String> {
@Override
protected String doInBackground(String[][]... voidThisArray) {
String voidThisString = null;
populateArray();
return voidThisString;
}
@Override
protected void onPostExecute(String voidThisString) {
setContentView(R.layout.main);
dialog.dismiss();
}
}
I would give you the LogCat for the force close but for some reason the LogCat isn't displaying anything...
Upvotes: 1
Views: 503
Reputation: 11439
You want to use an AsyncTask to first: create a ProgressDialog that will display the loading message. Then the AsyncTask
will work in the background collecting all of the data.
Hope that helps.
private class Task extends AsyncTask<Void,Integer, Void> {
private ProgressDialog dia;
@Override protected void onPreExecute() {
dia = new ProgressDialog(MyContext.this);
dia.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dia.setMessage("Loading. Please wait...");
dia.setCancelable(false);
dia.show();
// Set up preserver download stuff
}
@Override protected Void doInBackground(Void... voids) {
// perform server download stuff
}
@Override public void onProgressUpdate(Integer... prog) {
if (prog == null)
return;
dia.setProgress(prog[0]);
}
@Override protected void onPostExecute(Void voids) {
// Do any post op stuff
dia.cancel();
}
}
Upvotes: 3
Reputation: 1719
Look at AsyncTask and execute
populateArray();
in doInBackground method of AsyncTask.
And call
setContentView
only once you can show loading by using onPreExecute and onPostExecute method of AsyncTask
Upvotes: 2
Reputation: 9242
You should see this page for information about threading. Basically if your application is going to do any long running operation, you want it in a thread. Your UI is locking up because your performing this operation on your UI thread.
As far as progress updates, I personally use a ProgressDialog to show when something is happening, although you could build a custom view to indicate this.
Upvotes: 2