AppCompatActivity中未触发onBackPressed()方法 [英] onBackPressed() method not triggered in AppCompatActivity

查看:278
本文介绍了AppCompatActivity中未触发onBackPressed()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AppCompatActivity onBackPressed()方法无法在我的活动中触发.

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

我看到后退箭头按钮,并在按下它时获得了动画,但是什么也没有发生.同样,重写onKeyDown()具有相同的效果.它没有被呼叫.

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?

我的Activity.xml:

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

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

活动Java文件

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

推荐答案

在清单文件中的活动标签内定义以下内容:

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

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

之后,您的活动:

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

这篇关于AppCompatActivity中未触发onBackPressed()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆