π§© Question Body: I'm developing a Jetpack Compose app and ran into an issue where my HelloWorld() composable was not running, and the app crashed on launch.
While trying to import ComponentActivity, Android Studio shows two options:
androidx.core.app.ComponentActivity
androidx.activity.ComponentActivity
I wasn't sure which one to choose, and I initially selected the first one (androidx.core.app). Later, I found out that caused the crash because setContent {} for Compose wasn't working.
β My Question: What's the difference between these two ComponentActivity classes?
Which one should be used for Jetpack Compose and why?
π‘ What Worked for Me: Switching to androidx.activity.ComponentActivity (from androidx.activity:activity:1.10.1) fixed the crash and made Compose work properly.
π§ Code Sample (Before - crashing):
import androidx.core.app.ComponentActivity // β wrong
β Code Sample (After - working):
import androidx.activity.ComponentActivity // β correct
I tried to create a simple Jetpack Compose app with a HelloWorld() Composable function. In my MainActivity, I imported ComponentActivity, but Android Studio showed two options: one from androidx.core.app and one from androidx.activity. I wasnβt sure which one to pick, so I selected androidx.core.app.ComponentActivity. I expected the app to show βHello, World!β on the screen, but instead, it crashed on launch. Later I found that switching to androidx.activity.ComponentActivity fixed the issue and the Composable displayed correctly.
The key difference is in the package location and intended usage:
androidx.activity.ComponentActivity
(Recommended) ->
This is the modern, current implementation & part of the AndroidX Activity library
Designed specifically for modern Android development
Provides better integration with other AndroidX libraries like Navigation, Lifecycle, and ViewModel
androidx.core.app.ComponentActivity
(Legacy/Internal)
This is part of the AndroidX Core library & generally intended for internal use or backward compatibility
Less commonly used in typical app development & may not have all the modern conveniences and integrations
Which should you use?
Use androidx.activity.ComponentActivity
for new Android projects. It's the standard base class for activities in modern Android development and provides the foundation for using:
Jetpack Compose (via ComponentActivity
)
Activity Result APIs
OnBackPressedDispatcher
Lifecycle-aware components
ViewModels with proper scoping
The androidx.activity version is what you'll see in most current Android documentation and examples, and it's what Google recommends for new development.
I hope this helps