Home > OS >  onBackPressed() not called / Android
onBackPressed() not called / Android

Time:07-22

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>

CodePudding user response:

Try writing like this

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

CodePudding user response:

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") 
  • Related