in Education by
I am trying to use a BottomNavigationView with Android Navigation to create a standard tabbed application with three fragments: StatusFragment, MapFragment, and AlertsFragment. I have my main_activity.xml with a FragmentContainerView and BottomNavigationView defined as follows: <?xml version="1.0" encoding="utf-8"?> My main_nav_graph.xml: <?xml version="1.0" encoding="utf-8"?> .ui.status.StatusFragment" android:label="StatusFragment" /> .ui.map.MapFragment" android:label="MapFragment" /> .ui.alert.AlertsFragment" android:label="AlertsFragment" /> .ui.main.MainActivity" android:label="main_activity" tools:layout="@layout/main_activity" /> And the associated main_menu.xml: <?xml version="1.0" encoding="utf-8"?> In the onCreate method of my MainActivity, I configure navigation as follows: override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.main_activity) binding = MainActivityBinding.inflate(layoutInflater) val navHostFragment = supportFragmentManager.findFragmentById(R.id.mainNavHostFragment) as NavHostFragment val navController = navHostFragment.navController val appBarConfiguration = AppBarConfiguration( setOf( R.id.statusFragment, R.id.mapFragment, R.id.alertsFragment ) ) NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration) NavigationUI.setupWithNavController(binding.bottomNavigationView, navController) } The tab bar is displayed as expected, but tapping on the buttons does not do anything. I've made sure that the ids defined in main_menu.xml match those in main_nav_graph.xml. For being such a basic component of navigation on mobile, getting this to work on Android is proving to be frustratingly difficult; any help would be greatly appreciated. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Regarding the Tag: Unfortunately, both current answers are wrong and it is actually discouraged by Google! Stop using the tag. It is deprecated and will not be maintained anymore. You are correct to switch to which on top contains some lifecycle fixes that are not available in Your code looks almost perfect and I am pretty sure that it is not the navigation components, but the way you inflate the binding. That said, here is a working code sample from one of my apps. Take special care that you: create the Binding using the DataBindingUtil set the desired fragment as startDestination this one was correctly spotted by @sercan! give you element an id (not mandatory, but to be safe) use exactly the same imports! androidx.* everywhere, but material.bottomnavigation.BottomNavigtaionView. the id of the menu item must match exactly the id of the fragment in the navi graph (you already mentioned that in your question. But I still add it here for completness.) the FragmentContainerView really is the defaultNavHost for the given Activity. activity_main.xml ActivityRepository import androidx.databinding.DataBindingUtil import androidx.navigation.NavController import androidx.navigation.fragment.NavHostFragment import androidx.navigation.ui.setupWithNavController import com.google.android.material.bottomnavigation.BottomNavigationView private lateinit var navView: BottomNavigationView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) DataBindingUtil.setContentView(this, R.layout.activity_repository).also { binding -> binding.lifecycleOwner = this binding.repositoryViewModel = repositoryViewModel binding.webHookViewModel = webHookViewModel } setupNavController() } private fun setupNavController() { val navHostFragment = supportFragmentManager.findFragmentById(R.id.activity_repository_nav_host) as NavHostFragment val navController = navHostFragment.navController navView = findViewById(R.id.activity_repository_bottom_nav) NavigationUI.setupWithNavController(navView, navController) } nav_graph_repository.xml menu_repo_bottom.xml <?xml version="1.0" encoding="utf-8"?> ...

Related questions

0 votes
    I want to calculate browser height without considering browser address bar and bottom navigation bar height. The ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I want to calculate browser height without considering browser address bar and bottom navigation bar height. The ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I have a fragment with a view and an options menu: public class OfferingsFragment extends Fragment { public ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I have a fragment with a view and an options menu: public class OfferingsFragment extends Fragment { public ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 23, 2022 in Education by JackTerrance
0 votes
    Which method to use while working with XML fragments, instead of XML()? (a) XMLInterface() (b) ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 23, 2021 in Education by JackTerrance
0 votes
    Which one of the following switching is not transparent? A. Circuit switching B. Packet switching C. Both the options D. None of the above...
asked Jan 8, 2023 in Technology by JackTerrance
0 votes
    In which one of the following switching, there will not be any dedicated connection between two hosts? A. Circuit switching ... switching C. Both the options D. None of the above...
asked Jan 8, 2023 in Technology by JackTerrance
0 votes
    Can anyone tell me what is Routing and Switching? Select the correct answer from above options...
asked Jan 18, 2022 in Education by JackTerrance
0 votes
    _______________ is used for changing color and font formats, inserting clip art or switching other formatting within the document. Select the correct answer from above options...
asked Nov 28, 2021 in Education by JackTerrance
0 votes
    The charging in packet switching is ______________ based? A. Packet B. Time C. Both the options D. None of the above...
asked Jan 8, 2023 in Education by JackTerrance
0 votes
    As the resources are reserved between two communicating end systems in circuit switching, ___________ is achieved. A. ... constant rate C. reliability D. store and forward...
asked Jan 6, 2023 in Technology by JackTerrance
0 votes
    So i want to switch the elements of an array with 32 integers "n" times. All elements should be on the next position and the last ... scanf("%d", &n) != EOF) { for(int j=0; j...
asked May 27, 2022 in Education by JackTerrance
0 votes
    Currently I try to switch from vim to emacs and have some difficulty while editing code. For most of ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    So i want to switch the elements of an array with 32 integers "n" times. All elements should be on the next position and the last ... scanf("%d", &n) != EOF) { for(int j=0; j...
asked May 7, 2022 in Education by JackTerrance
0 votes
    So i want to switch the elements of an array with 32 integers "n" times. All elements should be on the next position and the last ... scanf("%d", &n) != EOF) { for(int j=0; j...
asked May 4, 2022 in Education by JackTerrance
...