-1

I'm new to Android development and I wanted to ask what is the best solution here.

In my activity_main.xml I have some content and on the bottom there is <android.support.design.widget.BottomNavigationView> I set some icons and created listener for it. The Navigation contains simple Settings icon, and when it's clicked it should start the SettingsActivity. This navigation shoud be everywhere in the app.

Here comes my question : What is the best approach when I don't want to implement this BottomNavigationView in every single activity, implementing the same listener over and over again in every activity ? I've heard of using fragments instead of activity but these are now deprecated right? There must be a better way, implementing the Navigation over and over again with exact same code can't be right approach.

Froggo
  • 11
  • 5
  • 2
    "I've heard of using fragments instead of activity but these are now deprecated right?" It's perfectly OK to use Fragment from the Support Library. The only deprecated Fragment class is `android.app.Fragment`, see for example [this post](https://stackoverflow.com/questions/49446411/fragments-deprecated-in-android-p) – Bö macht Blau Oct 17 '18 at 18:26
  • Yes @0X0nosugar is right and for layout thing just make a separate layout for your bottom navigation bar and then use it anywhere in your app using the tag – Deepak Kumar Oct 17 '18 at 18:28
  • Soo.. the best approach to avoid duplicating this component - which is clearly wrong, is to use Fragments ? Or is there any other modern android way ? – Froggo Oct 17 '18 at 18:30

2 Answers2

0

While regular com.android.Fragment is deprecated, the support fragment class located in either androidx.fragment.app.Fragment is not deprecated, and is probably what it makes the most sense to use here.

So using the fragment approach, you will have one Activity class with the BottomNavigationView set up. When the user navigates using the BottomNavigationView, you'll change which fragment is being shown in your Activity. This tutorial should be a good starting point for how to enable this functionality.

0

Here comes my question : What is the best approach when I don't want to implement this BottomNavigationView in every single activity, implementing the same listener over and over again in every activity

You won't need to do that since Fragments are here to help and they're not depreciated but, replaced with AndroidX (new) one: androidx.fragment.app.Fragment. Instead of implementing it over and over again, replace the new Fragment when the other item selected.

Use setOnNavigationItemSelectedListener then do your stuff.

Check the sample here.

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108