Russell Harrower
Russell Harrower

Reputation: 845

back button won't go back

In my Kotlin app, I have a back button that from what I understand works from the AndroidManifest.xml file

Inside that file I have the following

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

when the user is on the Catch Up screen they should be able to press the back arrow and it "should" go back to MainActivity - But it seems not to be working.

I am wondering what have I missed if anything

here is the full 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>

RESPONSE TO ANSWER

I also tried

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

But by it's self does nothing.

Upvotes: 1

Views: 921

Answers (2)

Zain
Zain

Reputation: 40830

they should be able to press the back arrow

Assuming that there is a miss understood between the bottom navigation back button. And the top arrow (which is called parent or UP button).

To handle that override onOptionsItemSelected in PodcastActivity: and finish the activity to get to the parent activity from the back stack..

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)
}

Upvotes: 0

Gabe Sechan
Gabe Sechan

Reputation: 93569

That doesn't do anything to the back button. That's a control for the up button on your appbar. The back button is handled completely differently. It will take the top of the fragment stack off (if one exists). If not it will call finish on the main activity. To override this behavior you have to override onBackPressed() of the Activity.

Upvotes: 1

Related Questions