Home > Net >  Activity embedding vs Fragments, when to use each one?
Activity embedding vs Fragments, when to use each one?

Time:12-09

Until now, fragments were the recommended solution for having one screen inside another main screen, for example for list-detail pattern.

Now google has released Activity embedding: https://developer.android.com/guide/topics/large-screens/activity-embedding

With Activity embedding APIs you can take advantage of the extra display area on large screens by showing multiple activities at once, such as for the List-Detail pattern, and it requires little or no refactoring of your app.

Is Activity embedding replacing fragments? or will fragments be a better solution for some cases? If so, for which cases will be better each solution?

Thank you

CodePudding user response:

Thanks for the post. I was not aware of this capability. As for your question, I think that last couple of lines of the link you posted answersit:

Modern android development uses a single-activity architecture with fragments, navigation components, and versatile layout managers like SlidingPaneLayout.

But if your app consists of multiple activities, activity embedding enables you to easily provide an enhanced user experience on tablets, foldables, and Chrome OS devices.

So, it looks like fragments are the way to go but, if your app is composed of activities that make sense to run side-by-side, then activity embedding may be useful.

CodePudding user response:

Is Activity embedding replacing fragments?

Not really.

(though composables are replacing fragments (and views)).

or will fragments be a better solution for some cases?

With respect to activity embedding, fragments are a better solution in most cases. Roughly 0% of Android devices today support activity embedding, whereas roughly 100% of Android devices support fragments (either through the modern Jetpack implementation or through the older framework implementation). Also, fragments are for more than merely side-by-side presentation on larger screens (e.g., as pages in a ViewPager).

Activity embedding is for developers who:

  • Have an existing app that is centered around activities, and
  • Want to adapt that app to deal with foldables and large-screen devices without rewriting the whole app to use fragments or composables, and
  • Are willing to live with the fact that activity embedding is for Android 12L and higher

(though I think activity embedding is also around for Android Automotive, the "your car runs Android" OS)

  • Related