[英]How to pass date from one fragment to another in the same activity in Android?
[英]2 similar looking Android UIs sharing the same Activity/Fragment?
我的 Android 应用程序中有 2 个不同的“页面/用户界面”,它们看起来非常相似,除了其中一个视图中缺少 TextInput 并且标题、下拉列表等不同。 这就是我的意思:
所以在这里我有意见。 第一个视图和逻辑已经存在,我必须为第二个视图创建一个新功能,该功能有点相似,但源于应用程序中的不同路径,并将表单提交到不同的端点。
我的问题:
我的同事建议只重用现有的FistViewActivity并隐藏“secondlabel...”和“secondInput...”并使用枚举来决定应用程序是在第一个流/路径中还是在第二个流/路径中,然后再决定提交端点基于此。
我是 Android 新手,但我认为与为第二个视图创建新的 Activity SecondViewActivity相比,这种方法并不好。 这是因为将两者结合到 FirstView 使其耦合和复杂(使用枚举来描述流程,决定提交哪个端点,提交前要检查哪些字段等)。
有什么想法吗? 我肯定是错的,但与“隐藏”和使用枚举来描绘路径相比,我更喜欢解耦。
可能需要问的第一个问题是设计问题:“我们首先需要两个如此相似的屏幕吗?”
如果你这样做,我认为在大多数情况下,这是一个好主意。 我对编程的看法是“如果你在复制和粘贴,你正在做一些可以抽象的事情”
你可以做的是有一个定义大部分行为的抽象类,然后你的两个实现类简单地定义区分行为。
如果您使用MVVM或MVP (我真的建议使用其中之一)-您可以做同样的事情,也可以使用完全不同的依赖项来模拟单独的行为。
class SharedViewModel: ViewModel() {
// Can share behaviour
}
class DistinctInstance {
//Multiple instances
}
abstract class AbstractActivity : AppCompatActivity() {
protected val viewModel: SharedViewModel by lazy {
SharedViewModel() //<-- only gets the SVM instance once, and only when accessed
}
abstract val distinctInstance: DistinctInstance
abstract fun someAbstractBehviour()
protected open fun OverrideableBehaviour() {
//You may need to do something different at some point
}
fun doesSomethingCommon() {
// actual implmentation
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
someAbstractBehviour()
}
}
我认为主要关注的领域不是创建“ BaseActivity ”的概念并确保您没有“ Lasagne code ” - 并牢记SOLID原则。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.