title: Changing a fragment on a button click within a fragment
body: <p>Iam trying to change a fragment when a button is clicked on that fragment. While doing so the fragment changes but the button  still appears on the changed fragment. Iam new to developing , someone please help me bug this issue.</p>
  
  <p>HomeFragment.java</p>
  
  <pre><code>import android.os.Bundle;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.TextView;
  
  import androidx.annotation.NonNull;
  import androidx.annotation.Nullable;
  import androidx.fragment.app.Fragment;
  import androidx.fragment.app.FragmentManager;
  import androidx.fragment.app.FragmentTransaction;
  import androidx.lifecycle.Observer;
  import androidx.lifecycle.ViewModelProviders;
  
  import com.vinodsharma.hcms_attendance_app.R;
  import com.vinodsharma.hcms_attendance_app.databinding.FragmentHomeBinding;
  import com.vinodsharma.hcms_attendance_app.fragments.NewFragment;
  
  public class HomeFragment extends Fragment {
  
      private HomeViewModel homeViewModel;
  
      FragmentHomeBinding binding;
  
      public View onCreateView(@NonNull LayoutInflater inflater,
                               ViewGroup container, Bundle savedInstanceState) {
  //        homeViewModel =
  //                ViewModelProviders.of(this).get(HomeViewModel.class);
  //        View root = inflater.inflate(R.layout.fragment_home, container, false);
  //        final TextView textView = root.findViewById(R.id.text_home);
  //        homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer&lt;String&gt;() {
  //            @Override
  //            public void onChanged(@Nullable String s) {
  //                textView.setText(s);
  //            }
  //        });
  //        return root;
  
          binding=FragmentHomeBinding.inflate(getLayoutInflater());
          return binding.getRoot();
      }
  
      @Override
      public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
          super.onViewCreated(view, savedInstanceState);
  
          binding.btnAttendance.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Fragment NewFragment=new NewFragment();
                  FragmentManager manager=getActivity().getSupportFragmentManager();
                  FragmentTransaction fragmentTransaction= manager.beginTransaction();
                  fragmentTransaction.replace(R.id.nav_host_fragment,NewFragment);
                  fragmentTransaction.addToBackStack(null);
                  fragmentTransaction.commit();
              }
          });
  
      }
  
      //*****To avoid memory leaks****8
      @Override
      public void onDestroyView() {
          super.onDestroyView();
          binding=null;
      }
  }
  </code></pre>
  
  <p>fragment_home.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout 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=".ui.home.HomeFragment"&gt;
  
      &lt;Button
          android:id="@+id/btn_goto_new"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginStart="8dp"
          android:layout_marginTop="8dp"
          android:layout_marginEnd="8dp"
          android:background="#00f"
          android:gravity="center"
          android:textColor="#ffff"
          android:textSize="20sp"
          android:text="Go To New Fragment"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent" /&gt;
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  
  <p>NewFragment.java</p>
  
  <pre><code>public class NewFragment extends Fragment {
      public NewFragment() {
          // Required empty public constructor
      }
  
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          // Inflate the layout for this fragment
          return inflater.inflate(R.layout.fragment_new, container, false);
      }
  }
  </code></pre>
  
  <p>fragment_new.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".fragments.NewFragment"
      &gt;
  
      &lt;!-- TODO: Update blank fragment layout --&gt;
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/hello_blank_fragment"
          android:layout_gravity="center"
          android:textColor="#ffff"
          android:textSize="24sp"/&gt;
  
  &lt;/FrameLayout&gt;
  </code></pre>
  
  <p>content_main.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout 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"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"
      tools:showIn="@layout/app_bar_main"&gt;
  
      &lt;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_constraintLeft_toLeftOf="parent"
          app:layout_constraintRight_toRightOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:navGraph="@navigation/mobile_navigation" /&gt;
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>