How to Save Fragment States with BottomNavigationView in Android?
Last Updated :
11 Dec, 2022
In Android applications, Activities and Fragments are from the foundation of the UI layer. It is now standard practice to load the UI with multiple fragments. For example, Instagram, Twitter, and a slew of other well-known apps. Imagine browsing some tweets in the Twitter app's HomeFragment, navigating to the search screen, and then returning to the Home Screen only to discover that the Home Screen has been reloaded and now displays the first tweet! This is not a good user experience because the user must scroll back to the previous tweet. In this article, we'll look at a simple method for dealing with and avoiding the recreation of these fragments, as well as saving their states. We'll use a BottomNavigation View because we'll be using multiple fragments on a single activity.
Let's Begin:
- Make a Project
- Begin by creating a new Android Studio project.
- Choose an empty activity and a name of your choice for the next step.
- Name of the package: Anything you want.
- Kotlin is the language.
- Finish
Your first step is now complete.
Before we add the Bottom Navigation view to our activity main.xml file, we must first create a menu resource for the icons and text that will be displayed on our bottom navigation view! The following is the Navigation.xml file from our menu resource folder:
XML
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_geeksforgeeks"
android:icon="@drawable/geeksforgeeks"
android:title="@string/geeksforgeeks" />
<item
android:id="@+id/navigation_afteracademy"
android:icon="@drawable/courses"
android:title="@string/after_academy" />
<item
android:id="@+id/navigation_user"
android:icon="@drawable/coursesName"
android:title="@string/coursesName" />
</menu>
Let's now include the bottom Navigation view component in our layout file!
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.activity.GfgMain">
<include
layout="@layout/main_gfg"
android:id="@+id/someContainer"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:background="?android:attr/windowBackground"
app:menu="@menu/navigation"
android:id="@+id/bottomNavigationView"/>
</androidx.constraintlayout.widget.ConstraintLayout>
The layout we've included here is the one we'll use to hold the fragment. So, let's get started on it.
What is the purpose of this app?
We will simply create a simple app with three fragments: GeeksforGeeksFragment, AfterAcademyFragment, and UserFragment. The GeeksforGeeksFragment, AfterAcademyFragment and UserFragment each have one like button and one text field for displaying the number of likes. The main goal of this app is to prevent fragments from being recreated when the user switches between them.
So, how to do it?
Let's make instances for our fragments as well as a global instance (let's call it activeFragment) that stores the currently viewed fragment.
Kotlin
private val geeksforGeeksFragment = GeeksforGeeksFragment()
private val afterAcademyFragment = AfterAcademyFragment()
private val userFragment = UserFragment()
private val fragmentManager = supportFragmentManager
private var activeFragment: Fragment = GeeksforGeeksFragment
Let's now add our fragments to the fragment manager:
Kotlin
supportFragmentManager.beginTransaction().apply {
add(R.id.someContainer, geeksforgeeksFragment, getString(R.string.gfg))
add(R.id.someContainer, afterAcademyFragment, getString(R.string.afterAcademy)).hide(afterAcademyFragment)
add(R.id.someContainer, userFragment, getString(R.string.user)).hide(userFragment)
}.commit()
Let's take care of onNavigationItemSelected on our BottomNavigationView component now:
Kotlin
NavBar.setOnNavigationItemSelectedListener { menuItem ->
when (menuItem.itemId) {
R.id.navigation_geeksforgeeks-> {
fragmentManager.beginTransaction().hide(activeFragment).show(geeksForGeeksFragment).commit()
activeFragment = geeksForGeeksFragment
true
}
R.id.navigation_afteracademy-> {
fragmentManager.beginTransaction().hide(activeFragment).show(afterAcademyFragment).commit()
activeFragment = afterAcademyFragment
true
}
R.id.navigation_user -> {
fragmentManager.beginTransaction().hide(activeFragment).show(userFragment).commit()
activeFragment = userFragment
true
}
else -> false
}
}
GeekTip: Remember that we have stored our current fragment in our activeFragment field, which should be visible to the user.
Now all we have to do is to hide and show what we have created in a well-designed manner, so as to prevent any mishaps!
show() and hide()
Instead of adding and replacing fragments (creating fragment instances), we use the helper functions show() and hide(), which do not destroy or recreate our fragments but instead hide and show them. Because the fragment instances are not destroyed, the user can see the previous state of the fragment!
Conclusion
You can use a recycler view to replace a fragment UI by loading a list of 50+ items, scrolling to a random position, switching between fragments, and returning. This logic can be applied to a UI that includes a Scroll View. We hope this article has been of some assistance to you.
Similar Reads
How to Hide/Show BottomNavigationView on Scroll in Android?
BottomNavigationView is the best option for navigation in Android. It makes life easier for a user to switch between multiple activities and fragments. It's really a pain in the butt to use an Android app without having proper navigation. At GFG, we have already shared an article with you on BottomN
3 min read
How to Save Switch Button State in Android?
In Android, a Switch is a type of button that lets the user toggle between two actions or instances. In general, a Switch is used for selecting one between two options that can be invoking any actions or functions. In this article, we are going to see how we can save the state of the Switch Button i
3 min read
BottomNavigationView in Android
BottomNavigationView makes it easy for users to explore and switch between top-level views with a single tap. There should be a minimum of 3 top-level views and a maximum of 5. If Destinations are more than 5 then use the Navigation Drawer. When the user taps on the icon it will change the top-level
5 min read
How to Communicate Between Fragments in Android?
Nowadays most apps have so many features so for that they use multiple fragments in a single app and communication is one of the important parts of apps for sharing data from one fragment to another because two fragments can't communicate directly. A Fragment represents a portion of any user interfa
4 min read
How to Create Fragment Using Bottom Navigation in Social Media Android App?
This is the Part 2 of "Build a Social Media App in Android Studio" tutorial, and we are going to cover the following functionalities in this article: We are going to Create Bottom Navigation with 5 Fragments (Home, Users, AddBlog, ChatList, Profile).On HomeFragment we will be Showing all the added b
4 min read
How to Create a New Fragment in Android Studio?
Android Studio is the official integrated development environment for Google's Android operating system, built on JetBrains' IntelliJ IDEA software and designed specifically for Android development. You can Develop Android App using this. Here, We are going to learn how to create a new Fragment in A
2 min read
View Binding with Fragments in Android Jetpack
In the Previous article View Binding in Android Jetpack, it's been discussed why acquiring the ViewBinding feature in the Android project provides a lot of benefits. But when it comes to ViewBinding with fragments the scenario changes. Because the lifecycle of the Fragment is different and that of a
6 min read
Curve Navigation Drawer in Android using ArcNavigationView
The Navigation Drawer is a layout that can be seen in certain applications, that consists of some other activity shortcuts (Intents). This drawer generally can be seen at the left edge of the screen, which is by default. A Button to access the Navigation Drawer is by default provided at the action b
2 min read
How to Pass Data from Dialog Fragment to Activity in Android?
In many applications, you may have seen that whenever we have to make choices some kind of elevated dialog box appears and ask the user for some input or choice. In this article, we are going to see the same that how we can pass data from a Dialog Box to activity in Android Studio. What we are going
4 min read
How to Implement findViewById in Fragments in Android?
Fragments represent a part of the app's UI which is reusable. Fragments are having its own layout, and lifecycle but must be hosted inside an activity to be visible. The activity becomes easier to modify if it consists of many fragments. A fragment can have multiple instances inside an activity. fin
3 min read