ThisCalculator

Everyday

Multi-Screen Android Views in One Host Activity

Updated · Standard formula, shown below

Multi-Screen Android Views in One Host Activity

Yes, you can build a multi-screen Android app with a single host Activity, and it is a common, recommended pattern for many apps. You manage the screens as separate fragments or custom views that the host activity swaps in and out, rather than creating a new activity for each screen.

Why Use a Single Activity?

Android apps traditionally used one Activity per screen. However, the single-activity architecture has become the standard for modern apps because it offers several concrete benefits:

  • Faster screen transitions: Swapping a fragment is lighter than starting a new activity, so the UI feels more responsive.
  • Shared UI state: The host activity can hold data that multiple screens need, avoiding repeated loading or passing large objects between activities.
  • Simpler navigation logic: You control the back stack and transitions in one place, making the app’s flow easier to reason about.
  • Better support for large screens: A single activity can show two panes side-by-side on a tablet or foldable, while the same code shows one pane on a phone.

Google’s own guidance and the Android Jetpack Navigation component are built around this idea. The recommended way to implement it is with Fragments and the Navigation Component, though you can also use custom views or even ViewFlipper for simpler cases.

How It Works: Fragments and the Navigation Component

Fragments are reusable UI components that live inside an activity. Each fragment has its own layout and lifecycle, but they are all hosted by the same activity. The FragmentManager handles adding, removing, and replacing fragments. The Navigation Component builds on this with a graph that describes the screens and the actions between them.

Here is a minimal setup for a single-activity app with two screens:

  1. Create a layout for the activity with a FragmentContainerView that will hold the current screen.
  2. Define a navigation graph XML file that lists the fragments (screens) and the actions that connect them.
  3. Use NavController in the activity to navigate to a destination, e.g. findNavController().navigate(R.id.action_first_to_second).
  4. Handle system back button presses with OnBackPressedDispatcher so the back stack works as expected.

Each fragment is a separate class with its own layout file. The host activity only sets the content view once and then delegates the rest to the navigation graph.

Alternatives to Fragments

Fragments are not the only way to have multiple screens in one activity. Depending on your needs, you might consider:

  • Custom Views: You can create a custom View for each screen and swap them in the activity’s layout. This is lighter than fragments but you lose the built-in lifecycle handling and back stack.
  • ViewFlipper / ViewSwitcher: These are simple containers that can show one of several child views at a time. They are useful for small, static screens but not for complex UIs.
  • Compose: If you use Jetpack Compose, you can manage screens with a NavHost composable that works similarly to the Navigation Component but without XML layouts. The single-activity principle still applies.
  • Dialogs and Bottom Sheets: Some “screens” can be implemented as dialogs or bottom sheets, which are also hosted by the activity.

For most apps, fragments with the Navigation Component are the best balance of flexibility and built-in support. For very simple apps, a custom view approach might be enough.

Common Problems and How to Avoid Them

Single-activity apps come with their own set of pitfalls. Here are the most frequent ones and how to handle them:

  • State loss on configuration change: When the device rotates, the activity is recreated. Use viewModel or savedInstanceState to preserve UI state. The Navigation Component automatically restores the back stack, but your fragment’s own data must be saved.
  • Back stack confusion: If you manually add fragments without using the Navigation Component, you must manage the back stack carefully. The Navigation Component handles this automatically.
  • Fragment transactions after onSaveInstanceState: You cannot perform a fragment transaction after the activity has saved its state. Use commitNow() or defer the transaction until after the state is restored.
  • Memory leaks: Holding references to the activity or fragment from long-running tasks can leak memory. Use lifecycle-aware components like LiveData or Flow with repeatOnLifecycle.
  • Nested fragments: If you have a fragment inside a fragment, use childFragmentManager instead of the activity’s supportFragmentManager.

When a Single Activity Is Not the Right Choice

While single-activity is great for most apps, there are cases where multiple activities make sense:

  • Deep linking: If you need a screen to be reachable directly from a URL or notification, an activity with an intent filter can be simpler than setting up deep links to fragments.
  • Independent modules: If a screen is a separate feature that could be reused in another app, it might be better as its own activity.
  • Complex transitions between unrelated screens: Sometimes a full-screen activity transition is what you want, e.g. for a video player or a camera view.
  • Legacy code: If you are maintaining an older app that already uses multiple activities, migrating to a single activity can be risky and time-consuming.

In those cases, the cost of a single-activity architecture may outweigh the benefits. The key is to choose the pattern that fits your app’s structure, not to follow a trend blindly.

Performance and Memory Considerations

Single-activity apps can be more memory-efficient because you are not creating a new activity for every screen. However, if you keep all fragments in memory (e.g., by using show/hide instead of replace), you may use more memory than necessary. The Navigation Component destroys fragments when they are not visible, which keeps memory usage low.

On the other hand, if you have many screens, the navigation graph can become large. Keep the graph organized by using nested graphs for each feature. Also, be mindful of the fragment’s onDestroyView – release any heavy resources like bitmaps or listeners there.

Frequently Asked Questions

Can I use multiple activities and still have a single host?

Yes, you can have a single activity that contains multiple fragments, but you can also have a hybrid approach where some screens are activities. For example, the main flow uses a single activity, but a settings screen is a separate activity. That is perfectly valid.

Do I need to use the Navigation Component?

No, it is optional. You can manage fragments manually with FragmentManager and FragmentTransaction. The Navigation Component just adds convenience, type safety, and automatic back stack handling. For very simple apps, manual management might be simpler.

What is the difference between a fragment and a view?

A view is a UI element like a Button or TextView. A fragment is a self-contained component that has its own layout, lifecycle, and can contain multiple views. Fragments are meant to be reusable and can be added to an activity’s layout or to another fragment.

Can I have a single activity with no fragments?

Yes, you can use custom views or ViewFlipper to switch screens. However, you will have to handle the back button and state saving yourself. Fragments are the standard because they solve these problems for you.

How do I pass data between screens in a single activity?

You can pass arguments to a fragment using Bundle or using the Navigation Component’s Safe Args. For shared data, use a ViewModel scoped to the activity or to the navigation graph. Avoid passing large objects in bundles; use a repository or a shared ViewModel instead.

Is a single activity app more secure?

Not inherently. Security depends on how you handle data and permissions, not on the number of activities. However, a single activity can make it easier to control navigation and avoid exposing unintended screens, but that is a minor factor.

Conclusion

Building a multi-screen Android app with one host activity is not only possible but is the recommended approach for most modern apps. Use fragments and the Navigation Component to keep your code organized, handle the back stack automatically, and support multiple screen sizes. Choose the pattern that fits your app’s needs, and be aware of the common pitfalls like state loss and memory leaks. With careful planning, a single-activity app can be clean, efficient, and easy to maintain.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.