Я использую 3 нижних актива навигации для 3 разных учетных записей, но по какой-то причине работает только одна. Это точно такой же автоматически сгенерированный код, но я изменил переменные et c. Учетная запись укрытия работает нормально, но для других учетных записей они показывают начальный фрагмент, но не переключаются между другими, когда нажимается нижняя навигация. После долгих размышлений я не уверен, что не так, и надеялся, что кто-нибудь узнает? Если нет, я постараюсь найти другой способ сделать это. Спасибо!
Это учетная запись ресторана, которая не работает. Класс:
public class RestaurantActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_restaurant);
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.restaurant_home, R.id.restaurant_account, R.id.restaurant_settings)
.build();
NavController navController = Navigation.findNavController(this, R.id.restaurant_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
}
}
Макет активности:
<?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/general_nav_menu" />
<fragment
android:id="@+id/restaurant_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0sp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/nav_view"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/restaurant_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
Макет навигации:
<?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/restaurant_home">
<fragment
android:id="@+id/restaurant_home"
android:name="com.example.feedthehomeless.RestaurantUI.home.HomeFragment"
android:label="@string/title_home"
tools:layout="@layout/restaurant_home" />
<fragment
android:id="@+id/restaurant_account"
android:name="com.example.feedthehomeless.RestaurantUI.account.AccountFragment"
android:label="@string/title_dashboard"
tools:layout="@layout/restaurant_account" />
<fragment
android:id="@+id/restaurant_settings"
android:name="com.example.feedthehomeless.RestaurantUI.settings.SettingsFragment"
android:label="@string/title_notifications"
tools:layout="@layout/restaurant_settings" />
</navigation>