1

I have a fragment with a RecyclerView in it. When clicking on an item, I'm navigating (with navigation component) to a detail fragment.

Inside the detail fragment, I want to implement the AppBarLayout. But I have already a Toolbar on activity level.

How can I achieve something like this? You can find the video here on Google design guidelines. Not sure if they are starting a new activity. Bottom navigation is also part of my activity and should be visible when navigating to detail fragment with AppBarLayout.

Detail Fragment

Recycler View List

Zain
  • 37,492
  • 7
  • 60
  • 84
Erkan
  • 140
  • 2
  • 11

1 Answers1

1

Inside the detail fragment, I want to implement the AppBarLayout. But I have already a Toolbar on activity level.

So, now the activity has a toolbar, and you want to set another toolBar in the DetailFragment. And as you're using navigation components, you are likely making the toolbar managed by the navController

Well, this requires you to move the activity level toolBar to be in the main fragment; that is hosted by navHostFragment.

The reason: because setting another toolbar from the fragment level will duplicate it as the activity level toolbar always persist. Check this answer for that.

And therefore you need to setup the toolBar in the fragments instead; and normally when you move from a fragment to another though the navigation components, no duplication will occur, and now you can have different toolbar as you'd like to.

Here is a schematic demo:

Activity:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    fun setupActionBar(toolbar: Toolbar) {
        setSupportActionBar(toolbar)
        val navHostFragment =
            supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
        val navController = navHostFragment.navController
        val appBarConfiguration = AppBarConfiguration.Builder(
            R.id.fragment, R.id.fragment_b
        ) .build()

        NavigationUI.setupActionBarWithNavController(
            this,
            mNavController,
            navController
        )
    }

}

FragmentA

class FragmentA : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_a, container, false)

        (requireActivity() as MainActivity).setupActionBar(view.findViewById(R.id.fragment_toolbar))


        return view
    }

}

FragmentB: Similar to FragmentA, but has its own toolBar

class FragmentB : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_b, container, false)

        (requireActivity() as MainActivity).setupActionBar(view.findViewById(R.id.fragment_toolbar))


        return view
    }

}
Zain
  • 37,492
  • 7
  • 60
  • 84
  • This is a solution but in my opinion not a good one. You have to setup the toolbar everytime when navigating to a different fragment. Even when I need only once a different toolbar in my whole application. – Erkan Sep 17 '21 at 17:45
  • @Erkan There is another option to have another activity for the detail fragment in the `navGraph`, and you can `setSupportActionBar` for that activity. [navigation components can support multiple activities](https://developer.android.com/guide/navigation/navigation-create-destinations#create-activity) – Zain Sep 17 '21 at 18:13
  • Thanks, I'll try it. It's against the single activity approach but currently this is the best solution for me. An found another solution too. Setting up the AppBarLayout on activity level and enable or disable scrolling behavior of the AppBarLayout. – Erkan Sep 18 '21 at 16:50