Simple ways to implement onBackPressed in fragment android

How to handle onBackPress in fragment by kotlin, latest way to handle backpress in fragments kotlin
How to handle onBackPress in fragment by kotlin, latest way to handle backpress in fragments kotlin
Handle onBackPressed in Kotlin

People keep asking that is there any way to handle backpress in a fragment or do we have any override method of onBackPress similar to an activity in fragment also.

Now I will say yes we have.

I am going to share with you 3 ways to handle backpress in fragment. I can use this methods in Java as well as Kotlin. And I will be using kotlin

Let’s Start:

Method 1: Officially available in android lifecycle


Create custom horizontal calendar using simple recyclerview in android using kotlin

Custom horizontal recyclerview calendar android in kotlin
Custom horizontal recyclerview calendar android in kotlin
Simple horizontal calendar using recyclerview

As you can see in the above image, we are going to create same horizontal calendar in simple way. So, let’s start

Let’s create a layout

Let’s start with creating layout

We need back arrow “<” for previous
“>” next arrow for next month
Text for showing the current selected month
And Last the recyclerview for showing the dates

Then we will be creating a row layout means recyclerview adapter layout which will have Day and Date in Text
With using built in style for text and fixed height & width

Now let’s create MainActivity.kt, In this we will have our main code

Tejas Soni

Passionate about learning, and sharing mobile development https://www.facebook.com/tejashsoni513 https://www.instagram.com/tejas_100ni/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store