Blank/black Screen On App Startup Due Data Downloading?
Solution 1:
You should probably to the download and parsing in the background and display some kind of splashscreen with progress information in the meantime. To avoid an annoying splash screen, you could also cache the data and display your app normally on startup, and only refresh the data once the bakground update is finished.
There are several options to do the download and parse operations in the background :
I cannot say what's the best solution in your specific case, but I would recommend reading the Processes and Threads and service documentation.
Solution 2:
Her goes your Async Task Class
classAsyncClassextendsAsyncTask<String,Void,String>
{
int result;
Context context;
ProgressDialog bar;
AsynclassListener<String> listener;
publicAsyncClass(Context context, AsynclassListener listener) {//add more parameter as your method body has (i.e Database db, int num) . Don't forget to initialize them.this.context=context;
this.listener=listener;
bar = newProgressDialog(context);
bar.setIndeterminate(false);
//make your progressBar here I have just given a simple example for above PB there are more parameters to set.
}
protectedStringdoInBackground(String... Param){
try{
result = storeData();//call your method here
}catch(Exception e){
// Do something when crash
}
return""+result;
}
@OverrideprotectedvoidonPreExecute() {
// TODO Auto-generated method stubsuper.onPreExecute();
bar.show();// By the time your data fetching and parsing will go on you this progress bar will be visible.
}
@OverrideprotectedvoidonPostExecute(String result) {
// TODO Auto-generated method stubsuper.onPostExecute(result);
bar.dismiss();//As soon as the work is complete the this method is called.
listener.onTaskComplete(""+result);
/**
* In your case you can later typecast back in integer once you recieve the result.
* this listener will post the result to your main activity.
*/
}
}
Here is your Interface
publicinterfaceAsynclassListener<T>{
publicvoidonTaskComplete(T result);
}
Now Let your Activity (Splash Class) implement the interface This will implement the method as :
@OverridepublicvoidonTaskComplete(String result) {
// here the asynclass will post the result as 1 or -1 whatever you want. //After that you may proceed with your next part i.e switching to next activity or so.
}
Edit: I forgot to mention about how this will be called :
newAsyncClass(getApplicationContext(), this).execute("");// here you have to enter the database and other parameter values that will be required to run the method. Change it accordingly.
As you can see here in your method you are fetching the data from net and parsing also : There is again a second approach in which you can call the network cal in a separate thread and later the parsing can be done further on UIthread.
Also read about the Async Task Class so as to know about the arguments and the working of class.
Post a Comment for "Blank/black Screen On App Startup Due Data Downloading?"