androidandroid-fragmentsbackgroundfragmentmapfragment

Android fragment remain background


Thanks in advance!


Solution

  • Well I found out what happened. It is due to work with two different fragment managers (FragmentManager and SupportFragmentManager).
    Depend on the fragment type (support or normal) you want to remove or replace you have to use one or another fragment manager.
    I have created an enum to know what kind of fragment is active (in order to remove) and what kind of fragment is going to start (in order to replace).
    More or less I made a function like this:

    private void manageFragment (FragmentEnum nextFragment) {
    
            switch (activedFragment) {
                case SUPPORT:
                    getSupportFragmentManager().beginTransaction().remove(fragmentSupport).commit();
                    break;
                case NORMAL:
                    getFragmentManager().beginTransaction().remove(fragmentNormal).commit();
                    break;
            }
    
            activedFragment = nextFragment;
    
            switch (nextFragment) {
                case SUPPORT:
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragmentSupport).commit();
                    break;
                case NORMAL:
                    getFragmentManager().beginTransaction().replace(R.id.fragment_container, fragmentNormal).commit();
                    break;
            }
        }