Search code examples
androidandroid-fragmentsfragmentmanager

Strange behaviour replacing fragments in Android


I've been working for several months in an app. The version uploaded to the playstore works fine. Recently, doing some improvements, the whole app has started to work in a very strange way.

If you swith between fragments, after a while it does not inflate properly the fragments, putting always the content of the first one. So, if you switch the fragment, the action bar changes but the content is always the same (and you cannot interact with it).

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

I am going crazy as there is no log or trace whatsoever.

I am using the usual way of swithing fragments and the most intriguing part is that at the beginning everything works ok:

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();

Do anyone has any kind of idea what could be happening here? Thanks!

edit: I also have tried to clear the backstack in case it was the problem, but it doesn't change anything

solved: As I explain in my answer, I have discovered that is a problem related to a SwipeRefreshLayout. More information about this problem can be found at When switch fragment with SwipeRefreshLayout during refreshing, fragment freezes but actually still work


Solution

  • Ok, I found out the problem.

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

    Removing it, or disabling it setEnabled(false) solves all the problems.

    I hope it will help to anyone that is having the same problem!

    Another thread in SO that refers to the same problem: When switch fragment with SwipeRefreshLayout during refreshing, fragment freezes but actually still work