Why are there 2 different ComponentActivity classes?
Asked Answered
N

1

11

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?

Nebo answered 27/2, 2019 at 21:59 Comment(0)
M
22

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

Marris answered 27/2, 2019 at 22:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.