6

I have a very simple app that consists of three Fragments and a Bottom Navigation bar, created by using "New Project -> Bottom Navigation Activity" in Android Studio. The first Fragment holds a button, which should take me to the second Fragment, like if the middle button of the Bottom Navigation bar was clicked.

Fragment with Button

Is there a "standard" way to do this?

I have tried:

  • using launch(...) of the Navigation component, which seems to launch the Fragment with its own back stack and breaks the bottom navigation.
  • using setSelectedItemId(...) in different ways, which either results in an exception or breaks bottom navigation in different ways.

In this post, someone asks the exact same question, but it is marked as a duplicate. I fail to find the answer, especially concerning Navigation component.

Kai Hatje
  • 431
  • 6
  • 17

2 Answers2

14

Clicking the Button should have the same effect as if the user taps the corresponding item in the bottom navigation. So you need to call setSelectedItemId() on the BottomNavigationView. This can only be done in the Activity displaying the BottomNavigationView.

One option is to introduce a shared ViewModel with

  • a LiveData to be observed by the Activity
  • a function onButtonClicked() to be called by the OnClickListener of your Button which will update the LiveData

Once the LiveData observer fires, your Activity can call

 binding.navView.selectedItemId = R.id.navigation_dashboard

Please note that for passing information about events like this one should choose some data type which can be invalidated after use. See for example LiveData with SnackBar, Navigation and other events (the SingleLiveEvent case)

Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61
  • 1
    This works and is written very well :) Btw: there is now official documentation about [passing events via LiveData](https://developer.android.com/jetpack/guide/ui-layer/events). I do find the medium article easier to read and understand though. – Kai Hatje Mar 02 '22 at 08:09
  • How can I handle data passing here? – Sumit Shukla Aug 07 '23 at 12:18
  • @Sumit Shukla - please ask a new question to better describe your requirement :) – Bö macht Blau Aug 11 '23 at 12:43
-2

Paste this code from where you want to go to the second fragment

Fragment fragment = new DashboardFragment();
            FragmentManager fm = getActivity().getSupportFragmentManager();
            fm.beginTransaction().replace(R.id.frame_layout, fragment).commit();

For more information click here

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Abhishek Dutt Mar 02 '22 at 04:02
  • The title of the question and the question itself asks about changing a tab programmatically with Bottom Navigation and the [Android Navigation component](https://developer.android.com/guide/navigation/navigation-getting-started). How does this answer solve that question? – Kai Hatje Mar 02 '22 at 07:19