androidkotlinandroid-activityandroid-jetpack-compose

What's the difference between androidx.core.app.ComponentActivity and androidx.activity.ComponentActivity in Android?


🧩 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.


Solution

  • The key difference is in the package location and intended usage:

    androidx.activity.ComponentActivity (Recommended) ->

    androidx.core.app.ComponentActivity (Legacy/Internal)

    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:

    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