Posts

Showing posts from 2021

Store Key Value Pairs Using Jetpack Data Store

One of the ways we can store information on an Android device is by using Shared Preferences. These Shared Preferences help us save key-value pairs. A newer way of saving such key-value pairs is using the Jetpack library Data Store . The Data Store library saves the key-value pairs using coroutine blocks and provides the ability to read the saved preferences using the collect function available in the Kotlin Flow. In this article, we will take a look at the Data Store class available in the Jetpack library and how to save, read the data using it. The first thing we need to do is add the dependency in the module level build.gradle file. implementation "androidx.datastore:datastore-preferences:1.0.0-alpha06" Create an instance of the Data Store using context and by passing the file name to the createDataStore function. Android creates the data store file inside a folder named datastore and this folder will be available in the app's files directory. The name passed to th

Use Java8 APIs on Older Android Versions

Image
The less adoption of Java8 APIs in the Android Codebase is because of the lack of support on the older versions. Even though Java8 was released in 2014, it took a while for Google to resolve the issue. Existing Issue We will take an example to understand the issue in detail. Let's say we have an array list that contains countries and we need to check if the desired country is available in the list. The code to achieve it would look something like below. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // Find the country with name India val country = countryArrayList.stream().filter { it == "India" }.findFirst() if (country.isPresent) { // Country is present in the list } else { // Country is not present in the list } } else { // For loop to filter and find the item on the older version var countryPresent = false countryArrayList.forEach { if (it == "India") { // Country is present in t

Listen to Back Button Clicks in Fragment

Listening to the back button click event inside the fragments, comprised of a painful implementation of the interfaces and the onBackPressed method calls in both Activity and Fragment, prior to the introduction of the OnBackPressedDispatcher . But now, inside a Fragment, the back button behaviour can be controlled by listening to the callback returned by the OnBackPressedDispatcher . This callback listener is implemented inside the  onCreate overridden method. override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Listen to Back button clicks val callback = requireActivity().onBackPressedDispatcher.addCallback(this) { // Handle the back button event } } Use onBackPressedDispatcher method to get the OnBackPressedDispatcher and add the callback using the addCallback method. This callback method takes a lifecycle owner. Handle the back button events whenever the callback gets triggered. The same implementation can be done