FragmentTransaction takes content from the previous fragment

2

Good afternoon.

I'm using tabs, bottomnavigation; My problem is that when I go from a tab to a BottomNavigation option, the contents of the tab fragment remain on the screen.

This is the xml where everything is located, the TabLayout and the BOTTOMNAVIGATION (BottomBar).

<android.support.design.widget.AppBarLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/BarLayout"
      android:animateLayoutChanges="true"
    >

        <include
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             layout="@layout/toolbar_layout"
             app:layout_behavior="@string/appbar_scrolling_view_behavior"
             android:id="@+id/include" />


        <android.support.design.widget.TabLayout
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:id="@+id/TabLayout"
           app:tabMode="fixed"
           app:tabGravity="fill"
           app:tabTextColor="@color/blanco"
            app:tabIndicatorColor="@color/blanco"
         />

</android.support.design.widget.AppBarLayout>




 <FrameLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:id="@+id/frame"         
     app:layout_behavior="@string/appbar_scrolling_view_behavior"
     />


<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/ViewPager"
    android:layout_below="@+id/BarLayout"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    />

<com.roughike.bottombar.BottomBar
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:id="@+id/Bar"
    android:layout_gravity="bottom"
    app:bb_tabXmlResource="@xml/bottombar_tabs"
    app:bb_behavior="shifting"
    app:bb_showShadow="true"
    app:bb_activeTabColor="#fff"
    android:layout_below="@id/ViewPager"
    />

This is my activity where is the code to go from fragment and more.

public class Activity_Inicio extends AppCompatActivity {

BottomBar bar;
Toolbar toolbar;
TabLayout tabLayout;
ViewPager viewPager;
private AdaptadorViewPager adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity__inicio);


    toolbar = (Toolbar) findViewById(R.id.include);
    setSupportActionBar(toolbar);

    tabLayout = (TabLayout) findViewById(R.id.TabLayout);
    viewPager = (ViewPager) findViewById(R.id.ViewPager);
    bar = (BottomBar) findViewById(R.id.Bar);


    adapter = new AdaptadorViewPager(getSupportFragmentManager());
    adapter.AdaptadorViewPager(new FragmentLapidas(), "Lápidas");
    adapter.AdaptadorViewPager(new FragmentLozas(), "Lozas");
    adapter.AdaptadorViewPager(new FragmentTazas(), "Tazas");


    viewPager.setAdapter(adapter);
    tabLayout.setupWithViewPager(viewPager);



    bar.setOnTabSelectListener(new OnTabSelectListener() {
        @Override
        public void onTabSelected(@IdRes int tabId) {

            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction trans = fragmentManager.beginTransaction();


            switch (tabId){

                case R.id.tab_home:

                    FragmentHome home = new FragmentHome();
                    trans.replace(R.id.frame, home).commit();
                    toolbar.setTitle("Fotocerámica");
                    tabLayout.setVisibility(View.VISIBLE);

                    break;

                case R.id.tab_videos:

                    FragmentVideos videos = new FragmentVideos();
                    trans.replace(R.id.frame, videos).commit();
                    toolbar.setTitle("Videos");
                    tabLayout.setVisibility(View.GONE);

                    break;

                case R.id.tab_testi:

                    FragmentTestimonios testimonios = new FragmentTestimonios();
                    trans.replace(R.id.frame, testimonios).commit();
                    toolbar.setTitle("Testimonios");
                    tabLayout.setVisibility(View.GONE);

                    break;

                case R.id.tab_info:

                    FragmentInfo info = new FragmentInfo();
                    trans.replace(R.id.frame, info).commit();
                    toolbar.setTitle("Información");
                    tabLayout.setVisibility(View.GONE);

                    break;


            }
        }
    });
  }
}

When I go from a BottomNav option to another BottomNav option it works correctly; then summarizing my query, this would be: How to fix that, I mean that the fragment content of the tab that I select will no longer remain when I select a BottomNavigation option.

I hope you have explained me well.

I'll give you an example

The "Hello blank fragment" is the content of a tab and the "info" is the content of the "Info" option of BottonNav.

    
asked by Jorge Requez 07.03.2017 в 21:48
source

1 answer

0

After the Fragment transaction, use addToBackStack (null) so that you do not add any Fragment to the "backstack":

trans.addToBackStack(null);

with this when you make a transaction you indicate that it is not saved in the backstack, I think it is likely to store one over another so you see content of the previous one when making a new transaction.

    
answered by 08.03.2017 в 02:32