简体   繁体   English

将自定义对话框放在基本片段 android kotlin wrt 性能等中是否很好

[英]Is it GOOD to put Custom Dialog inside the base fragment android kotlin w.r.t perfomance,etc

I'm creating custom dialog in some Fragments but I came up with a base fragment to use there and then just call from CHILD Fragment.我正在一些片段中创建自定义对话框,但我想出了一个基本片段以在那里使用,然后只从 CHILD 片段调用。

For instance, I've 10 fragments out of which just 3 are using that.例如,我有 10 个片段,其中只有 3 个正在使用它。

Does it good to move dialogs with base fragment and I just no need to recreate it.移动带有基本片段的对话框是否很好,我不需要重新创建它。

Anything that affects here will be good to know for me对我有影响的任何事情都会很高兴知道

like cohesion, coupling, performance, architecture etc比如内聚、耦合、性能、架构等

This is a bit of an opinion-based subject.这是一个基于意见的主题。 But I think "composition over inheritance" is pretty universally accepted advice in the OOP world.但我认为“组合优于继承”是 OOP 世界中普遍接受的建议。

What you're doing doesn't matter one way or the other with respect to performance, but it's kind of a bad solution for code maintainability.就性能而言,您正在做的事情并不重要,但它对于代码可维护性来说是一种糟糕的解决方案。

Some relevant things to read:一些相关的事情要阅读:

To do this without inheritance in this case, you can make a top-level extension function with the relevant arguments that returns the dialog.在这种情况下,要在不继承的情况下执行此操作,您可以使用返回对话框的相关参数创建一个顶级扩展函数。 Example:例子:

fun Fragment.showANumberDialog(theNumber: Int): AlertDialog =
    AlertDialog.Builder(requireContext())
        .setTitle(theNumber.toString())
        .setPositiveButton(android.R.string.ok) {}
        .show()

Note: You should really be using DialogFragment instead of a bare Dialog so it is maintained after screen rotations and when returning to the app after it has been in the background for a while.注意:您确实应该使用 DialogFragment 而不是裸 Dialog,以便在屏幕旋转后以及在后台运行一段时间后返回应用程序时对其进行维护。 So the above example more appropriately would look like this:所以上面的例子更合适地看起来像这样:

fun Fragment.showANumberDialog(theNumber: Int) =
    ANumberDialogFragment().apply {
        arguments = bundleOf(NUM_KEY to theNumber.toString())
        show(childFragmentManager, ANumberDialogFragment.TAG)
    }

private const val NUM_KEY = "the number"

class ANumberDialogFragment : DialogFragment() {
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog =
        AlertDialog.Builder(requireContext())
            .setTitle(arguments.getString(NUM_KEY))
            .setPositiveButton(android.R.string.ok) {}
            .show()

    companion object {
        const val TAG = "ANumberDialogFragment"
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM