-1

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());
        }
    }
Neha Agarwal
  • 622
  • 7
  • 24
bircastri
  • 2,169
  • 13
  • 50
  • 119

1 Answers1

1

By turn you mean screen orientation? If so, I think that your problem might be that your Activity is being recreated with screen orientation. And with that, your AsyncTask.

Refer to this to handle runtime changes. http://developer.android.com/guide/topics/resources/runtime-changes.html

Lucas
  • 151
  • 4