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();
}
}
All callbacks registered via addCallback are evaluated when you call super. onBackPressed() . In Android 12 (API level 32) and lower, onBackPressed is always called, regardless of any registered instances of OnBackPressedCallback .
You can use this AppCompat as your main Activity , which can then be used to launch Fragments or other Activities (this depends on what kind of app you're building).
androidx.appcompat.app.AppCompatActivity. Base class for activities that wish to use some of the newer platform features on older Android devices. Some of these backported features include: Using the action bar, including action items, navigation modes and more with the setSupportActionBar API.
public final class OnBackPressedDispatcher. Dispatcher that can be used to register OnBackPressedCallback instances for handling the onBackPressed callback via composition. public class FormEntryFragment extends Fragment { @Override. public void onAttach(@NonNull Context context) {
onBackPressed() is not present in AppCompactActivity, You have to implement an InterFace KeyEvent.Callback and override onKeyUp method and check if the key is BackButton or you can extend ActionBarActivity which is child class of AppCompactActivity
I believe onBackPressed()
is only called when the physical back button is pressed. If you're attempting to catch the toolbar back button press (the navigation icon), try using the following snippet:
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case android.R.id.home: {
// Your code here
}
}
return (super.onOptionsItemSelected(menuItem));
}
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);
}
}
back_button on the actionbar
this will wok
Toolbar toolbar=(Toolbar) findViewById(R.id.appBar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP);
getSupportActionBar().setDisplayShowHomeEnabled(true);
@Override
public void onBackPressed() {
super.onBackPressed();
this.finish();
}
or set which activity load when click back
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.ParentActivity" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With