0
  • I am doing an Android application with several tabs, let say that have 2 tabs. One tab is an "android.support.v4.app.Fragment" that contains a few textfields and a button, and the other one is an "android.app.Fragment" that at the same time is a MapFragment.
  • The thing is that the first tap calls another "android.support.v4.app.Fragment" with cardview (also support) and they can be navigate between them with some hierarchy.
  • Now if first a click in the map tap, later on click the other tab and finally I click the button to go to the frame that contains cardviews, I can see these them but in the background I see also the MapFragment.
  • I don't know how to solve this. I have tried to use replace, remove, add, popBackStack, ... In addition I had tried to delete from the rootview the frame of the fragment, but nothing happened.
  • The fact is I am going to throw your laptop out the window.

Thanks in advance!

Erqox
  • 11
  • 4

2 Answers2

0

If you are using .addToBackStack("") than remove this. and for calling fragment every time user .replace(); method for all your fragment.

Piyush
  • 18,895
  • 5
  • 32
  • 63
Lovekush Vishwakarma
  • 3,035
  • 23
  • 25
  • I have 3 fragments as I said. Two tabs (does not have `.addToBackStack("")`, 1-support, 2- normal), and one more fragment that is called from the tab 1 (that is why this has `.addToBackStack("")`). – Erqox Oct 26 '16 at 06:32
0

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;
        }
    }
Erqox
  • 11
  • 4