Skip to main content

Controlling Back button Behavior in Fragment-- kotlin

when user navigates in an application android maintains back stack of destinations . This allows android to go back to previous destination when user clicks back button . some times we need to implement our own back button behavior for best user experience . For example if we are in a timer based quiz application . when the user clicks on the back button if we go back to the previous destination without stopping the counter , it will crash the application . So first we need to know whether the user pressed the back button accidentally or not by using a dialogue box . If the user want to go back to previous destination we need to stop the counter and  then go back to the previous destination.

We can control the back button hahavior byusing OnBackPressedDispatcher . This controls how back button events are dispatched to one or more onBackPressedCallback objects . callbacks are added using addCallback methods. exampe code is given below

val callback = requireActivity().onBackPressedDispatcher.addCallback(this) {

            if(((findNavController().currentDestination?.id)==R.id.qaFragment)){

               val builder = AlertDialog.Builder(context)
               builder.setMessage("do you want to exit ?")
                   .setPositiveButton("yes",
                       DialogInterface.OnClickListener { dialog, which ->
                           couuntDown.cancel()
                           findNavController().navigateUp()
                       })
                   .setNegativeButton("no", null)
               builder.show()

           }
            else{

               findNavController().navigateUp()
           }
        }
our application consists of many fragments , we only want to control the back button behavior of a single fragment. We find the particular fragment 
if(((findNavController().currentDestination?.id)==R.id.qaFragment)){
When the user click on the back Button in the qaFaragment , we show a conformation dialogue box asking "do you want to exit ?" . If the user clicks yes then we will stop the timer and go back to the previous destination.

you can download the full source code of this app from my Github

Comments

Popular posts

Android List View using Custom Adapter and SQLite

following is a simple applicaton to create ListView using  Custom adapter.screenshot of the application  is like this . ListView is not used in android Anymore . We use  RecyclerView  and  CardView   in Android RecyclerView Demo is available on the following link http://androidtuts4u.blogspot.in/2017/04/android-recyclerview-example.html RecyclerView with Cardview Example is available on the following link http://androidtuts4u.blogspot.in/2017/09/android-card-view-example.html The ListView below the submit button is populated using Custom Adapter.Data is stored and retrieved using SQLite databsase. you can download the source code of this project from  google drive   https://drive.google.com/folderview?id=0BySLpWhqmbbdUXE5aTNhazludjQ&usp=sharing click on the above link ->sign into  your  google account ->add this to your google drive -> open it in google drive and download it. To create a simple ...

Android External database : Using database in "assets" folder

The best way to create a database application in android is , using an exteranal database . ie, we can create database  using tools like N avicat  , SQLite Browser  and copy this database into our application .This  method is more flexible and easy than creating a database in our application . This is a simple application using external database file.Application will insert data into database and show data from the database in a customized list view.  Screenshots of this application are MainActivity This is the first activity of this application . When  Register Here button is pressed it will start Registration Activity Editing of the entries can be done with a Long Click on the listView items. you can download the source code of this project from  google drive  https://drive.google.com/folderview?id=0BySLpWhqmbbdY3JHMkZRZFN0bEE&usp=sharing click on the above link ->sign into  your  google account ->a...

How to Open Android Emulator form Command Line and install Apk in Emulator

  You can also view this on my youtube channel How to Open Android Emulator from Commad Line 1.Open Command line 2.Change working directory to android sdk directory    cd  appdata/local/android/sdk/emulator 3.List all available Android virtual devices     emulator -list-avds 4. All your avds will be shown choose the avd_name you want to open    emulator -avd avd_name   5.Your Android virtual device will open up How to install APk file to emulator Drag the APK to Android emulator it will install automatically