Home > other >  How to integrate a new Fragment, different from the navigation bar Fragments and without replacing t
How to integrate a new Fragment, different from the navigation bar Fragments and without replacing t

Time:09-02

Having passed through multiple solutions on this site, while being new to navigation Fragments and Android in general I have this issue:

  1. I had 4 fragments that worked well together with the bottom navbar making the navigation;
  2. I added a favourite slides button on HomeFragment that leads to a FavouriteSlides Fragment that works;
  3. From the FavouriteSlides I still can access all 3 buttons, except Home. When I click the Home icon nothing happens. ( wrong)
  4. From all the other Fragments I cannot access Home Fragment because it is replaced by Favourite Slides Fragment ( wrong)
  5. BackArrow works Ok and all the Sliders are pointing ok to Home Fragment on Back Button pressed.

enter image description here

mobile_navigation.xml

<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/navigation_home">

    <fragment
        android:id="@ id/navigation_home"
        android:name="com.example.spiritualvietnam.ui.fragments.HomeFragment"
        android:label="HomeFragment"
        tools:layout="@layout/home">
        <action
            android:id="@ id/action_navigation_home_to_favouritesFragment"
            app:destination="@id/favouritesFragment" />
    </fragment>
    <fragment
        android:id="@ id/navigation_places"
        android:name="com.example.spiritualvietnam.ui.fragments.PlacesFragment"
        android:label="PlacesFragment"
        tools:layout="@layout/fragment_places_recyclerview">
        <action
            android:id="@ id/action_navigation_places_to_navigation_home"
            app:destination="@id/navigation_home" />
    </fragment>
    <fragment
        android:id="@ id/navigation_settings"
        android:name="com.example.spiritualvietnam.ui.fragments.SettingsFragment"
        android:label="SettingsFragment"
        tools:layout="@layout/fragment_settings">
        <action
            android:id="@ id/action_navigation_settings_to_navigation_home"
            app:destination="@id/navigation_home" />
    </fragment>
    <fragment
        android:id="@ id/navigation_sliders"
        android:name="com.example.spiritualvietnam.ui.fragments.SlidingPhotosFragment"
        android:label="SlidingPhotosFragment"
        tools:layout="@layout/fragment_sliding_photos">
        <action
            android:id="@ id/action_navigation_sliders_to_navigation_home"
            app:destination="@id/navigation_home" />
    </fragment>
    <fragment
        android:id="@ id/favouritesFragment"
        android:name="com.example.spiritualvietnam.ui.fragments.FavouritesFragment"
        android:label="FavouritesFragment" >
        <action
            android:id="@ id/action_favouritesFragment_to_navigation_home"
            app:destination="@id/navigation_home" />
    </fragment>
</navigation>

bottom_nav_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@ id/navigation_home"
        android:icon="@drawable/ic_home_icon"
        android:title="@string/home" />

    <item
        android:id="@ id/navigation_sliders"
        android:icon="@drawable/ic_lampion_play"
        android:title="@string/sliders" />

    <item
        android:id="@ id/navigation_places"
        android:icon="@drawable/ic_baseline_place_24"
        android:title="@string/places" />

    <item
        android:id="@ id/navigation_settings"
        android:icon="@drawable/ic_baseline_add_alert_24"
        android:title="@string/add_alerts" />

</menu>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.constraintlayout.widget.ConstraintLayout
        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="@drawable/gradient4navbar"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:menu="@menu/bottom_nav_menu" />

        <fragment
            android:id="@ id/nav_host_fragment_activity_main"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:layout_constraintTop_toTopOf="parent"
            app:navGraph="@navigation/mobile_navigation" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

Main Activity

class MainActivity : AppCompatActivity() {

    // Binding nu uita - baga click pe layout la becul galben si zi sa adauge data binding k omu
    private lateinit var binding: ActivityMainBinding
    lateinit var tinyDBMain: TinyDB

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        requestWindowFeature(Window.FEATURE_NO_TITLE)
        this.window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
        supportActionBar?.hide()  // hides title and stuff

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val navView: BottomNavigationView = binding.navView
        val navController = findNavController(R.id.nav_host_fragment_activity_main)

        val appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.navigation_home, R.id.navigation_sliders, R.id.navigation_places, R.id.navigation_settings,
            )
        )
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)
        // works here for all the fragments
        tinyDBMain = TinyDB(this)
    }

It's ok to answer in Java as well if you know the answer. If I should include more code I will I wanted to keep it as short as I could. Thank you

CodePudding user response:

Your appBarConfiguration and your menu items might be the reason why its not working you need to have your favoritesFragment id there, like this:

val appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.navigation_home,
                R.id.navigation_sliders,
                R.id.navigation_places,
                R.id.favouritesFragment
            )
        )

and like this:

<item
        android:id="@ id/favouritesFragment"
        android:icon="@drawable/ic_baseline_add_alert_24"
        android:title="@string/add_alerts" />

Generally the id from the fragments inside the navigation graph and inside the menu items has to be the same so that it works properly.

You also don't need to have actions inside your navigation graph, since the library, when usign a bottom navigaiton will automatically create the navigation, by using this configuration.

Let me know if this worked for you.

CodePudding user response:

I have found out that making the navbar disappear in the FavouritesFragment (that hasn't got an Icon in the navbar) is the best solution.

I implemented in Main Activity onCreate() the custom navbar behaviour for all the Fragments.

The answer that indirectly helped me, after days of searching, is here: how to hide BottomNavigationView on android-navigation lib

  • Related