【问题标题】:Why onBackPressed() is not fired when touching Back-Button / Android为什么在触摸 Back-Button / Android 时不会触发 onBackPressed()
【发布时间】:2022-08-03 14:45:04
【问题描述】:

为了重现我的问题,我做了一个小项目。它有两个活动。

这里是MainActivity.kt文件:

package me.soft.trybackbtnaction

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }


    fun fireSubActivity(view: View) {
        val intent = Intent(this, SubActivity::class.java).apply {}
        startActivity(intent)
    } /* End of fireSubActivity */
}

这里是子活动.kt文件:

package me.soft.trybackbtnaction

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log

class SubActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sub)
    }


    override fun onBackPressed() {
        super.onBackPressed()
        println(\"onBackPressed CALLED\")
    }
}

这是AndroidManifest.xml文件:

<application
    android:allowBackup=\"true\"
    android:dataExtractionRules=\"@xml/data_extraction_rules\"
    android:fullBackupContent=\"@xml/backup_rules\"
    android:icon=\"@mipmap/ic_launcher\"
    android:label=\"@string/app_name\"
    android:roundIcon=\"@mipmap/ic_launcher_round\"
    android:supportsRtl=\"true\"
    android:theme=\"@style/Theme.TryBackBtnAction\"
    tools:targetApi=\"31\">
    <activity
        android:name=\".SubActivity\"
        android:exported=\"false\"
        android:parentActivityName=\".MainActivity\" />
    <activity
        android:name=\".MainActivity\"
        android:exported=\"true\">
        <intent-filter>
            <action android:name=\"android.intent.action.MAIN\" />

            <category android:name=\"android.intent.category.LAUNCHER\" />
        </intent-filter>
    </activity>
</application>

下面要完成的是活动 XML 文件。

activity_main.xml

<?xml version=\"1.0\" encoding=\"utf-8\"?>
<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=\".MainActivity\">

    <Button
        android:id=\"@+id/button\"
        android:layout_width=\"wrap_content\"
        android:layout_height=\"wrap_content\"
        android:onClick=\"fireSubActivity\"
        android:text=\"Fire Sub-Activity\"
        app:layout_constraintBottom_toBottomOf=\"parent\"
        app:layout_constraintEnd_toEndOf=\"parent\"
        app:layout_constraintStart_toStartOf=\"parent\"
        app:layout_constraintTop_toTopOf=\"parent\" />
</androidx.constraintlayout.widget.ConstraintLayout>

activity_sub.xml

<?xml version=\"1.0\" encoding=\"utf-8\"?>
<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=\".SubActivity\">

    <TextView
        android:id=\"@+id/textView\"
        android:layout_width=\"wrap_content\"
        android:layout_height=\"wrap_content\"
        android:text=\"Hello Sub-Activity\"
        android:textSize=\"34sp\"
        app:layout_constraintBottom_toBottomOf=\"parent\"
        app:layout_constraintEnd_toEndOf=\"parent\"
        app:layout_constraintStart_toStartOf=\"parent\"
        app:layout_constraintTop_toTopOf=\"parent\" />
</androidx.constraintlayout.widget.ConstraintLayout>

运行应用程序并点击子活动中的后退箭头时,我希望onBackPressed()要调用的函数,但这不会发生。

  • 你检查过其他 Activity 生命周期方法吗?他们被称为?
  • 您是否尝试过使用新的onBackPressedDispatcher
  • @Faisal ur Rehman。 onCreate、onResume、onStop、onDestroy 都被调用。只有出于某种未知原因的 onBackPressed 才不会被调用。
  • @DarShan。不,我没有尝试过。我需要做些什么才能让它工作吗?
  • 它是一个新的 api,但是您的 onBackPressed 应该可以工作,但您也可以尝试更新的 API。

标签: android kotlin


【解决方案1】:

您可以在适用于片段和活动的 AppCompatActivity 的较新版本上尝试新的 onBackPressedDispatcher

例子:

  1. 活动
    onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
        override fun handleOnBackPressed() {
            // handle back press here.
        }
    })
    
    1. 片段
    val dispatcher = requireActivity().onBackPressedDispatcher
    dispatcher.addCallback(this, object : OnBackPressedCallback(true) {
        override fun handleOnBackPressed() {
            // handle back press in fragments.
        }
    })
    

【讨论】:

    猜你喜欢
    • 2013-01-02
    • 2015-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-15
    • 2019-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多