Single activity: Why, when, and how (Android Dev Summit '18)

Android Developers · 166K views · 2018 · 39:16

Open on YouTube

Software architecture & designSoftware (other)talkdated

Why Stack Overflow recommends it

Stack Overflow users link it to back up advice to prefer one activity with many fragments, especially when answering questions about sharing data, ViewModel scope and Navigation component usage.

Recommended by 10 people across 12 different months, 2019 to 2023.

What it is

A 39-minute Android Dev Summit 2018 talk by Ian Lake of the Android team on moving apps to a single-activity structure built on the Navigation Architecture Component. It covers why to make the change, when it fits, and how to migrate.

What people said

Recommends Pedro Varela · 2019-01 · Stack Overflow · on “Implement navigation with the Navigation components”

The navigation architecture component is part of JetPack [LINK] is a new way to navigate your app. The main different is that before you have multiple activities in your application -still you can- to move to different content areas in your app, now with the nav arch component you can have only one activity and the rest of the features of your app is based on fragments, keeping the single activity as a the main container. In this link [this video] Ian Lake the creator of navigation architecture explains why we should use this approach. Also, here is the guide to Navigation Architecture [LINK] Give a look at these resources. Best regards. Pedro Varela

Recommends ianhanniballake · 2019-02 · Stack Overflow · on “Navigation Architecture Component navigation with only a Context”

As per the Implementing Navigation documentation [LINK] : Note: The Navigation components are designed for apps that have one main activity with multiple Fragment destinations. The main activity hosts the navigation graph and is responsible for swapping destinations as needed. In an app with multiple activity destinations, each additional activity hosts its own navigation graph. Therefore, no. There is no global NavController that exists for your entire app - each Activity would have its own NavController and associated navigation graph. However, there's no requirement that you switch your entire app over to Navigation all at once. As per the Migrate to Navigation documentation [LINK] , you

Recommends ianhanniballake · 2019-03 · Stack Overflow · on “How to send data between activities using ViewModel”

ViewModels are not shared across Activities - since you pass a different object to ViewModelProviders.of() , you'll get different ViewModel instances. This was specifically called out in the Single Activity: Why, When, and How talk [this video] as a reason to prefer a single Activity architecture in your app.

Recommends Thalis Vilela · 2019-07 · Stack Overflow · on “Sharing String's content among different classes in android”

Sharing data between fragments and activities is a very delicate matter. There's problems controlling the variables across lifecycle changes, propagating data changes, so on and so forth.. So, if you're starting a new app, or its a project still in the beginning, i strongly recommend you take a look at android Jetpack [LINK] Architecture components, check the concepts of Single Activity [this video] architecture, Viewmodel [LINK] , LiveData [LINK] and Databinding [LINK] . I really think thats the right way to do what you want. You will be fixed! Otherwise, @gemy845's answer looks just fine!

Recommends Aswath · 2019-11 · Stack Overflow · on “How to save intent and start from same position later”

Usually, Android doesn't recommend using multiple activities. Ideally, an activity is something that needs to be revealed to outside of the scope of your application through the manifest file. Do think over whether you need to use multiple activities vis-a-vis fragments. Single Activity: Why, When, and How [this video] , because when you do, it becomes much easier to retain and manage your user flows

Recommends ianhanniballake · 2020-04 · Stack Overflow · on “Adding Default Navigation Drawer to NewActivity”

You should go through the Navigation codelab [LINK] and watch the Single Activity talk [this video] for what you should be doing in 2020.

Recommends Sergei Zarochentsev · 2020-06 · Stack Overflow · on “Using tab layout without fragments”

No, usually activities fill whole screen. And now I think Single activity is officially recommended way ( [this video] ) But you can use Tabs with custom views. There is library that helps building View-based Android applications ( [LINK] )

Recommends Debarshi Bhattacharjee · 2020-08 · Stack Overflow · on “Question about BottomNavigationView in Android”

1)```You should watch this video``` [this video] ```to understand better about Single Activity and Navigation Components``` and 2)regarding the ```backend thing```, ultimately you'll be working with apps that talk to servers and get data from the server, databases/files, from your phone or clouds. Nothing is static, data has to be loaded from network or database or files. That is why, for showing same type of data, example - menu of a restaurant, you should make a single fragment which has common UI for all menus and show the data accordingly

Recommends Tobi · 2020-11 · Stack Overflow · on “MVVM how do i update row that doesnt contain viewmodel?”

If you start learning MVVM in 2020, you should consider learning a Single-Activity approach. There you only have one activity, multiple fragments and one (shared) ViewModel for each fragment. Google for shared ViewModel and you will find a lot. I suggest to watch this android dev video: [this video]

Recommends MatPag · 2021-07 · Stack Overflow · on “Can a View necessarily have only one ViewModel in android?”

There is no requirements for Fragments to have just 1 ViewModel . Usually you have at least 2, the activity one (accessible to all child fragments) and the fragment one but there is no limit except that having multiple ViewModel s for the same fragment increases the business logic complexity and should be avoided. If you check a Google reference app like iosched [LINK] you can see that they use the fragment one and the activity one (where needed). If you use the single Activity pattern it would be easier, more info in this GoogleIO/2018 video [this video]