简体   繁体   中英

How come the onBackPressed() in a toolbar doesn't react?

I am trying to implement the onBackPressed() in my toolbar to go back to the MainActivity.

I used it in another project and it worked without fail. In this project the back arrow doesn't react.

The MainActivity is the parent of the other activity (Manifest).

EDIT: With the override onBackPressed function the back arrow reacts after multiple taps..

Many thanks

  if (happyPlaceDetailModel != null) {
        setSupportActionBar(binding.toolbarHappyPlaceDetail)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        supportActionBar?.title = happyPlaceDetailModel.title

        binding.toolbarHappyPlaceDetail.setNavigationOnClickListener {
            onBackPressed()
        }

Layout file:

<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=".activities.HappyPlaceDetailActivity">

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar_happy_place_detail"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    android:theme="@style/CustomToolbarStyle"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" /> 

Manifest:

<application
    android:allowBackup="true"
    android:icon="@drawable/happy_place_background"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.HappyPlaces">
    <activity android:name=".activities.HappyPlaceDetailActivity"
        android:parentActivityName=".activities.MainActivity"
        android:theme="@style/CustomNoActionBarTheme"
        android:label="Happy Place details"/>
    <activity
        android:name=".activities.AddHappyPlaceActivity"
        android:label="Add a Happy Place"
        android:theme="@style/CustomNoActionBarTheme" />
    <activity android:name=".activities.MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

sorry for the belated answer.

When you set your toolbar like this:


override fun onCreate(savedInstanceState: Bundle?) {
        setSupportActionBar(tb_chat_profile)
        supportActionBar?.apply {
            setHomeButtonEnabled(true)
            setDisplayHomeAsUpEnabled(true)
        }
}

You need to handle the click event on the Activity menu hook:

override fun onOptionsItemSelected(item: MenuItem): Boolean =
        when(item.itemId) {
            android.R.id.home -> {super.onBackPressed(); true }
            else -> super.onOptionsItemSelected(item)
        }

This approach is a sort-of legacy approach, which don't get me wrong it works and it's still right!

But the modern thing apps do nowdays is manually adding the a Button inside the Toolbar in the layout xml file like so:


<!-- This is pseudo-code, pasting it is likely to not work and it's just for demo purposes -->
<!-- You can play with the location of the button with the toolbar's 'gravity' property --> 
<androidx.appcompat.widget.Toolbar
     android:id="@+id/toolbar">
     <ImageButton android:id="@+id/back"/>
</androidx.appcompat.widget.Toolbar>

And finally set a listener like how you would do it in any other case:


override fun onCreate(savedInstanceState: Bundle?) {
    binding.back.setOnClickListener { 
        /*assuming you're inside an activity*/
        finish() 
    }

}

Did you try using this?

override fun onSupportNavigateUp(): Boolean {
        onBackPressed()
        return true
}

I don't suggest you use ",."? it would be better to use ".?" because it can give you a NullPointerException.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM