I have an Android application that use an AsyncTask in one activity. It works. So if I try to turn the smartphone when the task is running, the progress dialog hidden and the AsyncTask died. Now I know to fixed this problem I must to implement this two method.
protected void onSaveInstanceState(Bundle outState) {}
protected void onRestoreInstanceState(Bundle savedInstanceState)
But I don't Know how to save the state of my AsyncTask.
This is the code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
apriConnessioneDB();
//recupero le impostazioni dell utenza
Impostazioni impostazioni = db.getImpostazioni();
if(impostazioni!=null){
dialog = ProgressDialog.show(
this,
"Login",
"Verifica dati in corso.");
syncLogin = new SyncLogin();
syncLogin.setDialog(dialog);
syncLogin.setCt(this);
syncLogin.setDb(db);
syncLogin.setUsername(impostazioni.getUsername());
syncLogin.setPassword(impostazioni.getPassword());
//syncLogin.setCt(this.getApplicationContext());
syncLogin.execute();
}
}
protected void onSaveInstanceState(Bundle outState) {
try{
super.onSaveInstanceState(outState);
final AsyncTask task = syncLogin;
}catch(Exception e){
Log.e("ERROR", e.getMessage());
}
}