如何在清单中只声明一个Android活动,但在启动时使用两个实现中的一个?

huangapple go评论63阅读模式
英文:

How to make a single Android activity in the manifest but use one of two implementations when launched?

问题

我的Android应用程序今天在清单中声明了一个主要活动,但设计团队希望构建一个在外观和功能上都非常不同的替代活动。我们希望基于功能标志执行A-B测试,以便一些用户在启动时看到旧活动,而一些用户在启动时看到新活动,我们可以远程控制这一点。我们已经有能力远程控制功能标志,值已经保存在SharedPreferences中。

在Android清单中保持单一活动,然后在显示任何内容之前选择两种不同实现的最佳方法是什么?

英文:

My Android app has one main activity today declared in the manifest but the design team wants to build a replacement that is very different in both look and function. We want to perform A-B testing based on a feature flag so some users see the old activity at launch and some see the new activity at launch and we can control this remotely. We already have ability to remotely control the feature flags today already; the value is saved in SharedPreferences.

What is the best way to keep a single activity in the Android manifest and then have it choose one of two different implementations before anything is displayed?

答案1

得分: 3

对于基于XML的应用程序,我在其中一个应用程序中实施的方法之一是创建了两个片段,并根据标志,在启动时简单地导航到所需的片段(Jetpack Navigation库可以使片段事务变得简单)。由于每个UI都将有其自己的独立实现,代码将更加清晰,如果应用程序基于MVVM架构,您还可以共享视图模型以根据您的需求共享业务逻辑。

类似地,在Jetpack Compose中,根据标志导航到可组合部分将是相当简单的。视图模型的工作方式与基于XML的应用程序相同。

英文:

For XML based apps, one of the method that I implemented in one of my app was that I created two fragments and based on the flag, I simply navigated to the required fragment on startup (Jetpack Navigation library can make fragment transactions simple). Since each UI will have its own separate implementation, code will be much cleaner and if the app is based on MVVM architecture, you can also share the view models to share the business logic depending upon your requirements.

Similarly, In Jetpack Compose, it would be a fairly simple to navigate to the composable based on the flag. View Models will work the same as in XML based apps.

huangapple
  • 本文由 发表于 2023年7月28日 03:18:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76782804.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定