https://kotlinlang.org logo
#compose
Title
# compose
s

Sebastian Neagrau

11/25/2020, 10:41 PM
Hey guys! Im new to Android, and Jetpack Compose for that matter, and I was wondering if I can use multiple activities with Jetpack compose, because what I’ve seen from the Compose Examples, most of the applications (if not all of them) have a single main activity, and they handle navigation with NavHosts and NavControllers. If it is possible though to use multiple Activities, when should I make a new Activity? (Also should fragments be used with Jetpack compose?)
c

Colton Idle

11/25/2020, 10:53 PM
I haven't used AAC compose navigation yet, but I have used plain old AAC nav with multiple activities and that works, but the general consensus is that apps should be single activities with either multiple fragments, views, or composables being swapped in and out. As to when you should create a new activity... it depends 😃 but generally you should see an activity as an entry point into your app. Most apps I've worked on lately are single activity.
a

Adam Powell

11/25/2020, 11:02 PM
if you have a python background, using separate activities for all of your internal navigation is a bit like using
os.system
to run other python scripts instead of importing stuff from them and calling it directly. Or on the web, kind of like creating an iframe to host some other code and posting messages back and forth. There are good reasons to set things up to be able to work that way, but it's a lot of complexity and a limited communication model that should be justified by the use case
👍 1
s

Sebastian Neagrau

11/25/2020, 11:06 PM
I think my confusion mainly comes from the android documentation
Most apps contain multiple screens, which means they comprise multiple activities. Typically, one activity in an app is specified as the main activity, which is the first screen to appear when the user launches the app. Each activity can then start another activity in order to perform different actions. For example, the main activity in a simple e-mail app may provide the screen that shows an e-mail inbox.
From there, the main activity might launch other activities that provide screens for tasks like writing e-mails and opening individual e-mails.
But yes, I did get both of your points. Thanks a lot!
c

Colton Idle

11/25/2020, 11:24 PM
I would say that those docs are outdated.
Here's a video from two people on the android team, where two years ago at google i/o (googles annual dev conf) where they say to "use single activity when possible"

https://youtu.be/IrMw7MEgADk?t=1704

and yeah... that was ~2.5 years ago. I'm pretty confident with just saying to go for a single activity until you have to deviate from that. 😄
s

Sebastian Neagrau

11/25/2020, 11:39 PM
If my application will use “registerForActivityResult” in order to Screen record, do you think that would be a good case to deviate from using a single activity? I feel like having only one activity will make the code look cluttered just because I want to have a screen recorder. But yes I’ve researched a bit too, and Single activity is definitely the way to go!
a

Adam Powell

11/25/2020, 11:46 PM
Generally single activity apps feature very, very small activity subclasses. They quickly bootstrap into other code that stays appropriately factored and do not grow very often.
3 Views