Martin Šuráb
Martin Šuráb

Reputation: 177

Android: Replace fragment while asynctask running fails

I have a main activity with a FrameLayout in which I dynamically change Fragments based on (reside) menu selection. Recently (maybe with a new SDK version?)

I observed that when an AsyncTask is running on the current Fragment (SwipeRefresh) and I want to replace it with another Fragment, the existing Fragment stays visible (onTop basically) and the new one is hidden (behind probably - I know it's there, as it displays dialogs or does stuff, just that it is not shown). On back pressed also goes back to the 'root' Fragment (titlebar changes texts, too), however, that one with the running async task keeps hanging in that FrameLayout forever (until app is closed). Any help is really appreciated. Fragment replacement:

getSupportFragmentManager()
.beginTransaction().replace(R.id.container, targetFragment, "fragment")             .setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).commit();

Upvotes: 0

Views: 247

Answers (2)

Martin Šuráb
Martin Šuráb

Reputation: 177

I found the solution here: https://stackoverflow.com/a/27073879/5181489. In the end it was not the async tasks as I thought, it seems that the problem was with the SwipeRefreshLayout and this fixed it when called in onPause:

if (refreshView != null) {
    refreshView.setRefreshing(false);
    refreshView.destroyDrawingCache();
    refreshView.clearAnimation();
}

Upvotes: 1

Clemens
Clemens

Reputation: 96

Did you try to shut down the running AsyncTask in the onPause or onStop callback of your fragment?

Upvotes: 0

Related Questions