I have a problem in Android, I am using a Bottom navigation, the problem is that when I change between fragments if it makes the change but the view but the view of the first fragment is still seen in the others even if it is changed.
What is inside the blue box is the view of the first fragment that would be the home, and what is in red is the view of the second fragment that is the dashboard.
The home fragment has a recycler view and cards view.
This is the XML of the MainActivity, here is the nav_host_fragment where I do the transaction:
<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" >
</com.google.android.material.bottomnavigation.BottomNavigationView>
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="43dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/nav_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is how I handle shard transactions.
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
final Fragment homeFragment = new HomeFragment();
final Fragment linksFragment = new DashboardFragment();
final Fragment quienesSomos = new NotificationsFragment();
final FragmentManager fragmentManager = getSupportFragmentManager();
Fragment active = homeFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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
navView.setOnNavigationItemSelectedListener(myOnNavigationListener);
fragmentManager.beginTransaction().add(R.id.nav_host_fragment, quienesSomos, "quienesSomos").hide(quienesSomos).commit();
fragmentManager.beginTransaction().add(R.id.nav_host_fragment, linksFragment, "linksFragment").hide(linksFragment).commit();
fragmentManager.beginTransaction().add(R.id.nav_host_fragment, homeFragment, "homeFragment").hide(homeFragment).commit();
Log.d("ACTIVE","Activo: "+active.getTag());
}
private BottomNavigationView.OnNavigationItemSelectedListener myOnNavigationListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.navigation_home:
fragmentManager.beginTransaction().hide(active).show(homeFragment).commit();
//fragmentManager.beginTransaction().replace(active.getId(), homeFragment).commit();
active = homeFragment;
Log.d("ACTIVE","Activo: "+active.getTag());
return true;
case R.id.navigation_links:
fragmentManager.beginTransaction().hide(active).show(linksFragment).commit();
//fragmentManager.beginTransaction().replace(active.getId(), linksFragment).commit();
active = linksFragment;
Log.d("ACTIVE","Activo: "+active.getTag());
return true;
case R.id.navigation_qsomos:
fragmentManager.beginTransaction().hide(active).show(quienesSomos).commit();
//fragmentManager.beginTransaction().replace(active.getId(), quienesSomos).commit();
active = quienesSomos;
Log.d("ACTIVE","Activo: "+active.getTag());
return true;
}
return false;
}
};
}
I don't know why the first displayed fragment is not removed when switching to other fragments.
This is the XML of the home fragment, where my ListView is loaded:
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView 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:id="@+id/list"
android:name="com.seccion.seccion15.ui_session.inicio_session.InicioSessionItemFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
app:layoutManager="LinearLayoutManager"
tools:context=".ui.home.HomeFragment"
tools:listitem="@layout/fragment_home_item_list" />
And this is where I load my CardView to be displayed in the list:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
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="120dp"
android:layout_marginTop="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="0dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/titulo_post"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="Titlo de la noticia"
android:textColor="#000000"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imagen_noticia"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/vista_previa"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:maxLines="3"
android:text="Vista previa"
android:textColor="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imagen_noticia"
app:layout_constraintTop_toBottomOf="@+id/titulo_post" />
<TextView
android:id="@+id/fecha_post"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="15dp"
android:text="Fecha del post"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imagen_noticia"
app:layout_constraintTop_toBottomOf="@+id/vista_previa" />
<ImageView
android:id="@+id/imagen_noticia"
android:layout_width="102dp"
android:layout_height="95dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@tools:sample/avatars" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
I already found a solution, I only had to change
MainActivity
the fragment for aFrameLayout
:That worked for me.
I asked you to add
nav_host_fragment
and I see that it is aFragment
, remember that if you add a Fragment it cannot be replaced by a transaction :You must add a
Framelayout
there to perform the Fragments transaction: