【问题标题】:Why are there 2 different ComponentActivity classes?为什么有 2 个不同的 ComponentActivity 类?
【发布时间】:2019-02-27 21:59:11
【问题描述】:

我正在尝试通过addOnBackPressedCallback 注册OnBackPressedCallback 来处理片段中的后退按钮按下以接收onBackPressed() 回调,而无需覆盖您的活动中的方法(描述为Nov 5, 2018 release notes for androidx.activity

如果您查看AppCompatActivity Docs,它声称AppCompatActivity 扩展自androidx.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"

其他人可以重现这个问题吗?我是不是做错了什么?

【问题讨论】:

    标签: android androidx android-ktx


    【解决方案1】:

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

    您需要切换到 androidx.appcompat:appcompat:1.1.0-alpha02 以引入 Fragment 1.1.0,这会将 FragmentActivity(因此,AppCompatActivity)更改为扩展 androidx.activity.ComponentActivity

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-14
      • 2022-07-22
      • 2016-12-27
      • 2018-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-26
      相关资源
      最近更新 更多