0

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();
Martin Šuráb
  • 167
  • 2
  • 5

2 Answers2

1

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();
}
Community
  • 1
  • 1
Martin Šuráb
  • 167
  • 2
  • 5
0

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

Clemens
  • 86
  • 8
  • Thanks for the suggestion, I thought this was the issue. Well, I try, I am not sure how successfully but I do call `cancel()` on task in `onStop()` and check for `isCancelled()` within the async task. – Martin Šuráb Sep 26 '16 at 22:56