Replacing fragments quickly causes a weird screens

2019-07-27 05:04发布

问题:

I'm using a HomeActivity which replaces fragments dynamically and adds it to content frame layout dynamically as the user selected items from the drawer. On each fragment, it loads a feed from the network which it then renders. Here is the code I use for the switch -

    public void loadFragment(Fragment frag, String tag) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.content_fragment, frag, tag);
        try {
            fragmentTransaction.commit();
        } catch (IllegalStateException e) {
            logger.error(TAG, "Failed to commit fragment transaction... ", e);
        }
    }

Now if I switch between these fragment from the drawer (DrawerLayout is being used) and I do not let the network request for each fragment complete, somehow the new fragment which is replaced is placed underneath a screenshot of the old fragment while it was loading. Since the old fragment was showing values from cache, it shows a partial feed.

Now if you notice the top of this stack has the feed from the new fragment and it has layers as it should but the bottom of this stack has what appears to be a screenshot of the previous fragment and this seemingly screenshot / partial draw in the root view is z-indexed higher than the new feed for some reason and overshadows it. It also does not interact and interestingly enough I'm able to scroll the new feed which is z-index below (though is the highest layer in the picture above) even with the overlay.

I've tried to traverse the view hierarchy from the root view and print everything out and I was not able to find the text of the screenshot elements (which is why I think its an image or some sort of optimization being done by a third party library? We're using SwipeToRefresh also). I'm really confused as to what this and how do I get rid of it. This doesn't happen if I switch the fragments slowly and let it load. Any clues?