1

I've an Activity A that contains a Login Fragment and an Activity B that contains a Home Fragment. I've to start B from Login Fragment after a succesfully login request (async). I've a callback listener inside the login fragment:

onSuccess(result) {
    startActivity(B);
}

Today I met this nice bug: getting exception "IllegalStateException: Can not perform this action after onSaveInstanceState".

I think that's not properly a bug, anyway I don't know how to workaround that. This blog post suggests to avoid transaction inside async callback methods, yeah but how? commitAllowingStateLoss() should be used as a last resort: in case, should I use it inside Home Fragment transaction in Activity B creation method?

Basically, what should I do to start another activity after async callback?

Community
  • 1
  • 1
Jumpa
  • 4,319
  • 11
  • 52
  • 100

1 Answers1

0

You should use onPostExecute(result) in the AsyncTask:

private class LoginTask extends AsyncTask<parameters,...> {
...
protected void onPostExecute(Long result) {
         //if result successful start ActivityB
     }
}

Onpost fires after the asynctask is complete. It runs on the UI thread so that should solve your problem.

Put this in your main activity:

public void  run(){
        //code you would normally have after task completes
    }

Then put this in your onSuccess:

mainactivity.runUIonthread()
Lukos
  • 712
  • 4
  • 5
  • Unfortunately I'm not using AsyncTask class provided by android framework, but loopj library: so I have a callback from a listener inside the fragment and the request is inside another class. Any way to simulate onPostExecute and on UI thread execution? Many thanks. – Jumpa Nov 12 '14 at 18:40
  • Edited my answer for loopj, let me know if it works. – Lukos Nov 12 '14 at 19:03