Kotlin - Handling Back Button Click in Navigation Drawer - Android
Asked Answered
C

2

0

I have implemented Navigation Drawer in android.

Working fine.

onBackPressed method is also implemented.

Working fine.

Now, I want same onBackPressed() method's functionality on Back Button click of Navigation Drawer. (Back Button is set in Navigation Drawer.)

I have set Back Arrow button as below :

// Set back button
    mFragmentManager.addOnBackStackChangedListener(FragmentManager.OnBackStackChangedListener {
        if (mFragmentManager.backStackEntryCount > 0) {
            supportActionBar!!.setDisplayHomeAsUpEnabled(true)
        } else {
            supportActionBar!!.setDisplayHomeAsUpEnabled(false)
            mToggle.syncState()
        }
    })

Now, to take the click of Back button I have implemented below method :

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    when (item.itemId) {
        android.R.id.home-> {
            Toast.makeText(this, "Back pressed", Toast.LENGTH_SHORT)
                .show();
            onBackPressed();
        }
    }
    return super.onOptionsItemSelected(item)
}

But, this toast inside is not displaying, instead it just opening my Navigation Drawer.

What might be the issue ? Thanks.

EDIT :

Below is the code to open Drawer i.e. as below :

mToggle = ActionBarDrawerToggle(
            this,
            mDrawer,
            toolbar,
            R.string.navigation_drawer_open,
            R.string.navigation_drawer_close
        )
        mDrawer.addDrawerListener(mToggle)
        mToggle.syncState()

Here, just set a ActionBarDrawerToggle to Navigation Drawer. So, Drawer open when I click on HumberIcon.

Below is the onBackPressed() method inside my Activity :

if (mDrawer.isDrawerOpen(GravityCompat.START)) {
            mDrawer.closeDrawer(GravityCompat.START)
        } else {
            if (mFragmentManager.backStackEntryCount > 0) {
                mFragmentManager.popBackStack()
                nav_view.setCheckedItem(R.id.nav_home_fragment)
                
            } else {
                if (mFragmentManager.findFragmentById(R.id.fragment_container) is SecondFragment) {
                    mFragmentManager.popBackStack()
                    nav_view.setCheckedItem(R.id.nav_home_fragment)
                } else {
                    super.onBackPressed()
                }
            }
        }
    }
Copula answered 6/9, 2020 at 4:0 Comment(3)
please post the code that you using to open drawerFaradmeter
I know how to go back. I just want to perform back navigation on click of d Back button at left top corner of Navigation Drawer.Copula
@Akki Edited Question. Please check in question for EDIT section. Thanks.Copula
F
2

If your toolbar is OK, you can use the following to go back one with NavComponent.

findNavController().popBackStack()

If there is a problem with your toolbar, please share the relevant codes.

Fez answered 6/9, 2020 at 8:32 Comment(1)
I know how to go back. I just want to perform back navigation on click of d Back button at left top corner of Navigation Drawer.Copula
P
1

Step 1: In your XML design your toolbar on top

<androidx.appcompat.widget.Toolbar
            android:id="@+id/myToolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingEnd="@dimen/_5sdp"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

Step 2: Then bind your top toolbar in top with text

 with(binding) {
            setContentView(root)
            setToolbar("Top Nav Bar").toString().lowercase()
            setToolbar() //<---- call this fun then (for back arrow in top left)
}

//===[ Handle your Top Left Back arrow button ]===
 fun setToolbar() {
        with(binding.appBar) {
            setSupportActionBar(myToolbar)
            supportActionBar?.setDisplayShowTitleEnabled(false)
            myToolbar.navigationIcon = drawableColor(R.drawable.ic_back,R.color.black)
            myToolbar.setNavigationOnClickListener { onBackPressed() }
            myToolbar.setTitleTextColor(Color.BLACK)
        }
    }

Then handle your onBackPressed() wherever you want to redirect.

E.G.:

     override fun onBackPressed() {
            super.onBackPressed()
            launchActivity<MainActivity>()
     }
Picker answered 21/3, 2023 at 13:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.