Baradi Baradari Baradi Baradari - 6 months ago 450
Android Question

onBackPressed() method not triggered in AppCompatActivity

AppCompatActivity onBackPressed() method fails to trigger in my activity.

I see the back arrow button and get the animation when pressing it, but nothing else happens. also overriding onKeyDown() has the same effect. it's not called.

I've spent many hours researching this with no luck. Nothing seems to work. Anyone has had a similar issue? Maybe this is a known bug?

My Activity.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.litenote.android.BackTestingActivity2Activity">
<include
android:id="@+id/appBar"
layout="@layout/app_bar" />
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />




app_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primary"
android:titleTextAppearance="@color/textWhite"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:popupTheme="@style/CustomPopupMenuTheme">




The activity Java file

package com.litenote.android;

import android.support.v7.app.ActionBar;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;

import com.superpad.android.R;

public class BackTestingActivity2Activity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_back_testing_activity2);
Toolbar actionBar = ((Toolbar) findViewById(R.id.appBar));
setSupportActionBar(actionBar);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP);
getSupportActionBar().setDisplayShowHomeEnabled(true);

}

@Override
public void onBackPressed() {
Log.d("BACK_BUTTON_DOESNT_WORK", "I will never execute and you will never see me :(");
super.onBackPressed();
this.finish();
}
}

Answer

In your manifest file define the following inside your activity tag:

<meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.ParentActivity" />

After that in your activity:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }