为什么会有2个不同的ComponentActivity类

为什么会有2个不同的ComponentActivity类

本文介绍了为什么会有2个不同的ComponentActivity类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过addOnBackPressedCallback注册OnBackPressedCallback以接收onBackPressed()回调来处理片段中的后退按钮,而无需在您的活动中覆盖该方法(描述为 2018年11月5日androidx.activity发行说明)

如果您查看 AppCompatActivity文档,它会声明AppCompatActivityandroidx.activity.ComponentActivity扩展.

我遇到的问题是,在我的代码库中,AppCompatActivity是从androidx.core.app.ComponentActivity(没有addOnBackPressedCallback())而不是androidx.activity.ComponentActivity(具有addOnBackPressedCallback())扩展的.

我的依赖版本

implementation "androidx.core:core-ktx:1.0.1"
implementation "androidx.appcompat:appcompat:1.0.2"
implementation "androidx.activity:activity-ktx:1.0.0-alpha04"

还有其他人可以重现此问题吗?我在做错什么吗?

解决方案

androidx.core.app.ComponentActivity是隐藏类,在将androidx.activity.ComponentActivity作为AndroidX Activity 1.0和Fragment 1.1.0的一部分创建之前使用过.

您需要切换到androidx.appcompat:appcompat:1.1.0-alpha02以插入片段1.1.0,该片段会更改FragmentActivity(并因此更改为AppCompatActivity)以扩展androidx.activity.ComponentActivity

I'm trying to handle back button presses in my fragments by registering an OnBackPressedCallback via addOnBackPressedCallback to receive onBackPressed() callbacks without needing to override the method in your activity (described Nov 5, 2018 release notes for androidx.activity)

If you look at the AppCompatActivity Docs, it claims AppCompatActivity extends from androidx.activity.ComponentActivity.

The problem I'm running into is, in my codebase, AppCompatActivity extends from androidx.core.app.ComponentActivity (doesn't have addOnBackPressedCallback()) instead of androidx.activity.ComponentActivity (has addOnBackPressedCallback()).

My Dependency Versions

implementation "androidx.core:core-ktx:1.0.1"
implementation "androidx.appcompat:appcompat:1.0.2"
implementation "androidx.activity:activity-ktx:1.0.0-alpha04"

Can anybody else reproduce this issue? Am I doing something wrong?

解决方案

androidx.core.app.ComponentActivity is a hidden class that was used prior to androidx.activity.ComponentActivity being created as part of AndroidX Activity 1.0 and Fragment 1.1.0

You need to switch to androidx.appcompat:appcompat:1.1.0-alpha02 to pull in Fragment 1.1.0, which changes FragmentActivity (and hence, AppCompatActivity) to extend androidx.activity.ComponentActivity

这篇关于为什么会有2个不同的ComponentActivity类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:17