Reputation: 483
I have a splash screen Activity appear for 10 second while waiting this time the Activity check if tables are created and all data are loaded form server if not it creates tables and load data to DB. every thing is OK but the problem is when loading data take more than 10 seconds the Splash Activity is finished and start another Activity how i can keep splash activity wait until all data are loaded here is my code
if(! (checkTables()&&checkData())){
progressDialog.show();
fillSamples();
fillExams();
fillQuestions();
fillSubQuestions();
createProfile();
}
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
progressDialog.dismiss();
Intent studentAccess = new Intent(SplashScreen.this,Samples.class);
startActivity(studentAccess);
finish();
}
},10000);
i am using volley StringRequest and ImageRequest to download data and images from remote server
Upvotes: 2
Views: 3007
Reputation: 739
You can use AsyncTask ,it is better for network calls by creating this inner class
private class Operation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
//do what ever operations you want
for (int i = 0; i < 3; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.interrupted();
}
}
return "result";
}
@Override
protected void onPostExecute(String result) {
Intent i = new Intent(SplashScreen.this, MainActivity.class);
i.putExtra("data", result);
startActivity(i);
finish();
}
@Override
protected void onPreExecute() {}
@Override
protected void onProgressUpdate(Void... values) {}
}
and execute the process using private method in splash activity like this
new Operation().execute("");
Upvotes: 3
Reputation: 4022
Network calls should be always called from not main thread.
What you do is that you are making your main thread sleep . Never sleep you main thread. Android will kill your app. Use AsyncTask for network calls (doInBackground) and onPostExectute() do all other things.
Upvotes: 1