1

Iam working on bottom navigation bar and I have replaced fragment on onClickListener of floatingActionButton but that fragment is not removed if other item in bottom navigation bar is selected and two fragments are shown at a time. This is my output

enter image description here

How can I remove this post fragment of floatingActionButton if other item in bottom navigation bar is selected.

This is my activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout  xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<fragment
    android:id="@+id/nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    app:layout_constraintBottom_toTopOf="@id/nav_view"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:navGraph="@navigation/mobile_navigation" />

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@string/app_name"
    app:backgroundTint="@color/white"
    app:layout_anchor="@id/bottomAppBar"
    app:maxImageSize="64dp"
    app:srcCompat="@drawable/ic_baseline_add_circle_24" />

<com.google.android.material.bottomappbar.BottomAppBar
    android:id="@+id/bottomAppBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    app:fabAlignmentMode="center"
    android:layout_gravity="bottom"
    app:contentInsetStart="0dp"
    app:fabCradleMargin="0dp"
    app:fabCradleRoundedCornerRadius="0dp"
    app:fabCradleVerticalOffset="0dp">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:labelVisibilityMode="labeled"
        app:menu="@menu/bottom_nav_menu" />
</com.google.android.material.bottomappbar.BottomAppBar>

 </androidx.coordinatorlayout.widget.CoordinatorLayout>

and this is my MainActivity.java

package www.example.xchangeit;


import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.viewpager.widget.ViewPager;

import www.example.xchangeit.ui.post.PostFragment;

public class MainActivity extends AppCompatActivity{


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.opening_page);
    ViewGroup LiView = findViewById(R.id.ViewGroup);
    LiView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        layout();

        }
    });

    ImageView imageView = findViewById(R.id.logo_imageView);
    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            layout();
        }
    });


    }

    private void layout()
    {
        setContentView(R.layout.activity_main);
        BottomNavigationView navView = findViewById(R.id.nav_view);

        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_exchange, R.id.navigation_post,R.id.navigation_chat,R.id.navigation_account)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.nav_host_fragment,new PostFragment());
                transaction.commit();}

        });

    }

   }
mayra
  • 73
  • 8

1 Answers1

0

You could pop the previous fragment before you load in the next

getSupportFragmentManager().popBackStack();

Now if you need to be more specific you can do something like:

getSupportFragmentManager().beginTransaction().remove(frag).commit(); 

In the second case we keep a reference to the fragment we wish to remove.

badvok
  • 421
  • 3
  • 11
  • It is not working because my fragments are called through `AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder( R.id.navigation_home, R.id.navigation_exchange, R.id.navigation_post, R.id.navigation_chat, R.id.navigation_account)` , so where should I write this line of code? – mayra Aug 20 '21 at 08:20
  • 1
    Your `nav_host_fragment` is the full size of the screen so I'm guessing the image you posted if from the launched `PostFragment` and that either has two text views or its own containers for 2 other fragments, which have to be `replaced` and `added` to respectfully. – badvok Aug 20 '21 at 08:38