Home > Blockchain >  Close and Replace Fragment from a tab and close current fragment
Close and Replace Fragment from a tab and close current fragment

Time:03-31

I have option menu, what im trying to do is: I want to select a tab using option menu then replace the Fragment of the target tab with another fragment.

the Image show the replacement fragment overlapped to the current fragment of the target tab.

help thanks

enter image description here

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    int _id = item.getItemId();

    // Select tablayout using option Menu
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout);
    TabLayout.Tab _tab;

  switch (_id)
  {
      case R.id.mnutabindx0Frag2: //show first tab  with second fragment 
          //Set the Tab index to show
           _tab = tabLayout.getTabAt(index:0);
          _tab.select();


          //Close the fragment of the target tab
          getSupportFragmentManager().beginTransaction().remove(new Frag_First()).commit();
          // set the Fragment to show
         getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                 new Frag__Second()).commit();

          break;
      case

CodePudding user response:

You do not need to call

//Close the fragment of the target tab

      getSupportFragmentManager().beginTransaction().remove(new Frag_First()).commit();

Since, replace call removes other added fragments of a same containerViewId

https://developer.android.com/reference/android/app/FragmentTransaction#replace(int, android.app.Fragment, java.lang.String)

CodePudding user response:

Ok I got it, First I preapare two different Fragment Container, ViewPager and FrameLayout. then I used FameLayout to handle the replacement Fragment, after that I set the visibility depend on the condition I made.

    //Fragment Container Visibility method
private void _setvisibility_FragContainers(FrameLayout fl, ViewPager vp,boolean flflag)
    {
        if(vp.getVisibility() == View.INVISIBLE & flflag ==false )
        {
           vp.setVisibility(View.VISIBLE);
           fl.setVisibility(View.INVISIBLE);
        }
        else if(fl.getVisibility() == View.INVISIBLE & flflag == true) {
          vp.setVisibility(View.INVISIBLE);
          fl.setVisibility(View.VISIBLE);
        }
    }


//Option menu
 @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int id_item = item.getItemId();



        switch (id_item)
        {
            case R.id.mnutabindx0Frag2: //show first tab and second fragment
              // select tablayout by option menu
                SelectTabUsingOptionMenu(tab_layout,0);
              // show FrameLayout with frame on it
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new Frag_Second()).commit();
                break;

            default:return false;
        }
// Clear the viewpager visibility
        _setvisibility_FragContainers(frameLayout_fragcon,view_pager,true);
  • Related