Strange behavior replacing fragments in Android

I have been working on the application for several months. The version uploaded to the play store works great. Having made some improvements recently, the whole application started to work very strangely.

If you move between fragments, after a while it doesn't inflate the fragments correctly, always putting the content of the first one. So, if you switch the fragment, the action bar will change, but the content will always be the same (and you won't be able to interact with it).

Here is a video of the error https://streamable.com/9exa

I'm going crazy because there is no log or traceback at all.

I use the usual harsh snippet method and the most intriguing part is that everything works fine in the beginning:

Fragment fragment = getSelectedFragment(index); // returns the desired fragment
FragmentManager fragmentManager = getFragmentManager();

//Pop backstack
for(int i = 0; i < fragmentManager.getBackStackEntryCount(); ++i) {
    fragmentManager.popBackStack();
}

fragmentManager.beginTransaction()
               .replace(R.id.container, fragment)
               .addToBackStack(null)
               .commit();

      

Does anyone have any idea what might be going on here? Thank!

edit . I also tried clearing the stack in case that was the problem, but it doesn't change anything

... As I explain in my answer, I found this to be a SwipeRefreshLayout related issue. More information on this issue can be found in When a Fragment Fragment with SwipeRefreshLayout during an update, the Fragment freezes but actually still works

+3


source to share


3 answers


Ok I figured out the problem.

It seems that SwipeRefreshLayout was causing all the problems. One of my fragments used it and after switching fragments a couple of times it gets stuck.

Removing or disabling setEnabled (false) solves all problems.



I hope this helps anyone who has the same problem!

Another thread on SO that links to the same issue: When a Fragment Fragment with SwipeRefreshLayout during update, the Fragment hangs but actually still works

+3


source


What snippets are you using?



I had some weird behavior using frames of frames and I switched to V4 and everything worked fine

0


source


Please check this link: FragmentManager popBackStack does not delete fragment

I am using the following code in my activity to switch between fragments and it works fine. My app minSdkVersion is 15.

    private void setFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction =
            fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.container, fragment);
        fragmentTransaction.commit();
    }

      

0


source







All Articles