Я создал navHost , который я хотел бы заменить фрагментами из моего navGraph и с помощью прослушивателя событий onNavigationItemSelected я хочу заменить navHost накаждый элемент навигации, который я выбрал.Но я до сих пор не знаю, почему метод FragmentTransaction.replace не заменяет navHost замененным navGraph на
content_main.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main">
<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"></fragment>
</androidx.constraintlayout.widget.ConstraintLayout>
mobile_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/mobile_navigation"
app:startDestination="@+id/nav_home">
<fragment
android:id="@+id/nav_home"
android:name="com.example.airtimeflip.ui.fragments.home.HomeFragment"
android:label="@string/menu_home"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/nav_profile"
android:name="com.example.airtimeflip.ui.fragments.profile.ProfileFragment"
android:label="@string/profile"
tools:layout="@layout/fragment_profile" />
<fragment
android:id="@+id/nav_manager"
android:name="com.example.airtimeflip.ui.fragments.manager.AccountManagerFragment"
android:label="@string/menu_slideshow"
tools:layout="@layout/fragment_account_manager" />
<fragment
android:id="@+id/nav_atc"
android:name="com.example.airtimeflip.ui.fragments.atc.AtcFragment"
android:label="@string/menu_gallery"
tools:layout="@layout/fragment_atc" />
<fragment
android:id="@+id/nav_buy_airtime"
android:name="com.example.airtimeflip.ui.fragments.buy_airtime.BuyAirtimeFragment"
android:label="@string/menu_tools"
tools:layout="@layout/fragment_buy_airtime" />
<fragment
android:id="@+id/nav_send_money"
android:name="com.example.airtimeflip.ui.fragments.send_money.SendMoneyFragment"
android:label="@string/menu_share"
tools:layout="@layout/fragment_send_money" />
</navigation>
MainActivity.java
package com.example.airtimeflip;
import android.os.Bundle;
import com.example.airtimeflip.config.GlobalUtilConfig;
import com.example.airtimeflip.ui.fragments.atc.AtcFragment;
import com.example.airtimeflip.ui.fragments.buy_airtime.BuyAirtimeFragment;
import com.example.airtimeflip.ui.fragments.home.HomeFragment;
import android.util.Log;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.GravityCompat;
import androidx.fragment.app.FragmentTransaction;
import androidx.navigation.NavController;
import androidx.navigation.NavDestination;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import com.example.airtimeflip.ui.fragments.manager.AccountManagerFragment;
import com.example.airtimeflip.ui.fragments.profile.ProfileFragment;
import com.example.airtimeflip.ui.fragments.send_money.SendMoneyFragment;
import com.google.android.material.navigation.NavigationView;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private static final String TAG = "MainActivity";
private AppBarConfiguration mAppBarConfiguration;
private DrawerLayout drawer;
// private NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
//adding event listener to the navigation view
navigationView.setNavigationItemSelectedListener(this);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_profile, R.id.nav_manager,
R.id.nav_atc, R.id.nav_buy_airtime, R.id.nav_send_money)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
int id = menuItem.getItemId();
menuItem.setChecked(true);
try {
if (id == R.id.nav_home){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.nav_host_fragment, new HomeFragment());
ft.commit();
} else if (id == R.id.nav_atc){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.nav_host_fragment, new AtcFragment());
ft.commit();
} else if (id == R.id.nav_buy_airtime){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.nav_host_fragment, new BuyAirtimeFragment());
ft.commit();
}else if (id == R.id.nav_profile){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.nav_host_fragment, new ProfileFragment());
ft.commit();
}else if (id == R.id.nav_manager){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.nav_host_fragment, new AccountManagerFragment());
ft.commit();
}else if (id == R.id.nav_send_money){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.nav_host_fragment, new SendMoneyFragment());
ft.commit();
}
} catch (Exception e) {
//e.printStackTrace();
Log.d(TAG, "onNavigationItemSelected: "+ e.toString());
}
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
}