Reputation: 4566
I've currently been implementing my BottomNavigationView which as 4 menu items by adding in the xml code to each of the 4 activities for the different menu items. Each of the 4 activities also have the same BottomNavigationView. However, each time I press a different button on the bottomNavigationView, the BottomNavigationView refreshes along with the change in activity. So, should I be using Fragments or Activities with BottomNavigationView? Each of the 4 activities I have are not related to each other.
I'm using the BottomNavigationViewEx library, but that shouldn't change anything. Here is the common XML between the 4 activities:
<com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx
android:id="@+id/bottom_navigation_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />
Upvotes: 3
Views: 1916
Reputation: 1049
I have done a lot of research on this myself trying to find a good answer because I would typically use fragments with one activity, however fragments can be very tricky to work with due to their more complex life cycle, compared to that of activities. So it is very common to have to deal with a lot of issues that stem from the complex life cycle of fragments.
So I was thinking what if you could make each fragment into its own activity with the bottom nav. Then to keep the the bottom nav in place by using activity transitions and making the bottom nav a shared element between the activities so everything else on the page could do whatever custom animation you wanted. Below are some links to the documentation and an example repo I found to demonstrate this.
https://android.jlelse.eu/android-material-design-activity-transition-55de706ab967 https://developer.android.com/training/material/animations.html https://github.com/ddekanski/BottomNavigationViewBetweenActivities
Now one thing I am unsure of if this matters to you is if you would like the user to be able to swipe between the pages in the app. If you said yes, then I would stick with fragments for now and use a ViewPager
to accomplish this. The swiping still may be possible with only activities, I just have yet to explore how to do it although this link might be a good start.
https://www.bignerdranch.com/blog/viewpager-without-fragments/
If you do choose to use fragments, this is also a great tutorial and covers everything you could want to know about bottom navigation.
https://android.jlelse.eu/ultimate-guide-to-bottom-navigation-on-android-75e4efb8105f
Upvotes: 1
Reputation: 10557
If the bottom menu is same for all of the 4 screens then yes use Fragments instead of Activities.. it will give you more flexibility and easier to maintain
You can use ViewPager to handle your fragments or FragmentTransaction with FrameLayout to display your Fragments.
Upvotes: 3