lundi 3 mars 2014

Splash screen with background task


Vote count:

0




I have a splash screen that loads URLs from the Internal Storage and downloads their content from the Web (with an AsynkTask). It puts the downloaded data into an ArrayList, calls the main Activity and finishes. The main activity adapter manages the ArrayList and sets a ListView containing its data.

While I'm in the main Activity, if I press the back button the application exits (I set the android:nohistory="true" for the splash screen activity), but when I return to the app, the splash screen gets loaded and downloads the data again, "doubling" the list view.

How can I prevent the splash screen to be loaded when I return to the app?


Splash screen code:



Context mContext;
ProgressBar progress = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
setContentView(R.layout.activity_launcher);

progress = (ProgressBar)findViewById(R.id.progress);
progress.setIndeterminate(true);


if(canWriteOnExternalStorage()) {

try {
setupStorage();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
//dialog appears


}


The setupStorage() method loads the file from the Storage and executes the AsynkTask.

Maybe could the overriding of the onRestart() method be a solution?

Or should I prevent the AsyncTask from loading the data already downloaded?

Thanks!



asked 1 min ago






Aucun commentaire:

Enregistrer un commentaire