0

I am testing a tiny Android App where I need to perform some action when a back button is pressed. I have prepared the following function in Kotlin:

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

I must be missing some detail, because the function is never called. I expect it to be called when the back button is pressed.

Any comment from a second eye may be helpful.

In case this may be useful, hereafter is the relevant code for the activity:

package me.soft.myapp

import ........

class ModifyActivity : AppCompatActivity() {
    private var sharedPreferences: SharedPreferences? = null
    private var constraintLayout: ConstraintLayout? = null
    .....

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_modify)

        sharedPreferences = getPreferences(MODE_PRIVATE)
        constraintLayout = findViewById(R.id.main)
        ......
    }


    override fun onStop() {
        super.onStop()
        println("onStop CALLED")
    }

    override fun onResume() {
        super.onResume()
        println("onResume CALLED")
    }

    override fun onDestroy() {
        super.onDestroy()
        println("onDestroy CALLED")
    }

    override fun onRestart() {
        super.onRestart()
        println("onRestart CALLED")
    }

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

--*******************************************--

In order to reproduce my issue I made a tiny project. It has two activities. And I checked that it still has the same problem.

Here is the MainActivity.kt file:

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 */
}

Here is the SubActivity.kt file:

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

And this is the AndroidManifest.xml file:

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

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

</manifest>
Michel
  • 10,303
  • 17
  • 82
  • 179

2 Answers2

0

Try writing like this

override fun onBackPressed() {
    println("onBackPressed CALLED")
    super.onBackPressed()
}
Beant Singh
  • 169
  • 4
  • I have tried that too before, but it made no difference. – Michel Jul 21 '22 at 05:42
  • I added information to the post,please have a look. It should make it easy to reproduce the bug and hopefully make it clear where I am missing something. – Michel Jul 21 '22 at 17:10
  • have called this method manually? or you are trying to press the device's back button. if you are calling this method on some view click could add that code also? – Beant Singh Jul 22 '22 at 06:23
  • I am only touching the back button on the activity. This is where I expect the onBackPressed() to be called. – Michel Jul 24 '22 at 12:45
0

Try removing or commenting out this line :

super.onBackPressed()

also instead of:

println("onBackPressed CALLED")

try instead:

import android.util.Log
...
Log.v("onBackPressed","onBackPressed CALLED") 
Forrest
  • 109
  • 1
  • 8
  • I have tried your suggestions, with no result at all. – Michel Jul 21 '22 at 16:17
  • What happens when you press the back button ? Does the app close ? – Forrest Jul 21 '22 at 21:54
  • No the app does not close, but the onBackPressed() function is not called as I expect it should. – Michel Jul 22 '22 at 04:02
  • I made another post to cleanly reformulate the problem: https://stackoverflow.com/questions/73075124/why-onbackpressed-is-not-fired-when-touching-back-button-android – Michel Jul 22 '22 at 04:31
  • well, I have no clue what the problem might be, but is there any chance that you have some accessibility feature activated or you somehow remapped the back button through develeopper options? – Forrest Jul 23 '22 at 17:22
  • I don't think do; or at least not that I know of. – Michel Jul 24 '22 at 12:48