Trying to create a app that has 3 navigation buttons at the buttom and a new activity comes up when corresponding button is


Trying to create a app that has 3 navigation buttons at the buttom and a new activity comes up when corresponding button is



I'm trying to create a app that navigates in the same way movie pass app does. At the button it has 3 buttons that stay there and a new Activty comes up when you press the buttons.
I could put these buttons on all 3 activtes, but then if I make a code change to the buttons it must be changed in three places.




2 Answers
2



Two options could help with this. 1) Use a fragment for the content shown above the three buttons so you just have a single activity, and the buttons change which fragment is shown. 2) If you are using a BottomNavigationView or some other view that takes a listener you can create your own OnNavigationItemSelectedListener implmentation that handles all the logic for what happens on button presses. Then that code is in a single place, and you can use that listener in all three activities.


BottomNavigationView


OnNavigationItemSelectedListener


BottomNavigationView navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(new MyNavigationListener());



Use fragments with bottom navigation view it will sortout your problem.



if you use activities instead of fragments then you need to create bottom navigation view for each activity. it will increase your code length & complexity.



If you don't have idea how to use fragments with bottom navigation view. Please follow below link.



https://www.androidhive.info/2017/12/android-working-with-bottom-navigation/



I hope it will help you :)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API