【问题标题】:back button won't go back后退按钮不会返回
【发布时间】:2021-06-13 06:08:48
【问题描述】:

在我的 Kotlin 应用程序中,我有一个后退按钮,据我所知,它适用于 AndroidManifest.xml 文件

在该文件中,我有以下内容

 <activity
            android:name=".PodcastActivity"
            android:label="Catch Up">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>

当用户在 Catch Up 屏幕上时,他们应该能够按后退箭头,并且它“应该”返回到 MainActivity - 但它似乎不起作用。

我想知道我错过了什么

这里是完整的 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.drn1.drn1_player">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/drn1logo"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/drn1logo_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.DRN1"
        android:usesCleartextTraffic="true">

        <activity
            android:name=".MainActivity"
            android:configChanges="orientation"
            android:exported="true"
            android:launchMode="singleTop"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".PodcastActivity"
            android:label="Catch Up">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>
    </application>

</manifest>

对答案的回应

我也试过

override fun onBackPressed()
    {
        super.onBackPressed()
        val returnIntent = Intent(this, MainActivity::class.java)
        startActivity(returnIntent)
    }

但它自己什么都不做。

【问题讨论】:

    标签: android kotlin android-activity android-navigation


    【解决方案1】:

    这对后退按钮没有任何作用。这是您应用栏上向上按钮的控件。后退按钮的处理方式完全不同。它将关闭片段堆栈的顶部(如果存在)。如果不是,它将在主要活动上调用完成。要覆盖此行为,您必须覆盖 Activity 的 onBackPressed()。

    【讨论】:

    • 我无法判断为什么这不起作用,因为我不知道你做了什么。但这不会,这是错误的配置。
    【解决方案2】:

    他们应该能够按后退箭头

    假设底部导航返回按钮之间有一个错过的理解。以及顶部箭头(称为父按钮或 UP 按钮)。

    PodcastActivity 中处理覆盖onOptionsItemSelected:并完成活动以从后台堆栈到达父活动..

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> {
                finish() // back to the parent activity.
                return true
            }
        }
        return super.onOptionsItemSelected(item)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-15
      • 2017-08-30
      • 1970-01-01
      相关资源
      最近更新 更多