Fragment AsyncTask not executing after back button pressed

247 views Asked by At

My program is structured as follows from a navigation standpoint:

Fragment A -> Fragment B -> Fragment C (back button pressed) -> Fragment A

I have an async task that normally fires in onActivityCreated in Fragment A:

fetch = new FetchVenues(this, user);
fetch.execute();

After navigating to other fragments, I handle a back button press as follows:

getSupportFragmentManager().popBackStack(FIRST_FRAGMENT, 0);
getSupportFragmentManager().beginTransaction().commit();

This returns the program to fragment A. However upon returning to this fragment, onActivityCreated is called but my async task does not fire again.

I'm not sure why this is the case. I would like the async task to fire again.

2

There are 2 answers

1
Chetan On BEST ANSWER

There are two things I am worried about. I am not sure if the onActivityCreated() is being called again. Because as far as I can think, All the 3 fragments are in the same activity. Thus, the activity is not destroyed while changing the fragments and hence won't be created again on switching back to fragment A.

Second, one AsyncTask can be executed only once. This isn't the case here. But I think, if fetch = new FetchVenues(this, user); is written somewhere else and only fetch.execute(); is written in onActivityCreated(), then it can be a problem too.

0
ThaiPD On

fire asyn task in onCreateView() of fragment instead of onCreate of activity because onCreate() be called only one in the first time.

See the life circle of fragment in here