活动可以访问片段以了解是否已按下按钮吗? [英] Can an Activity access a Fragment to know if a button has been pressed?

查看:59
本文介绍了活动可以访问片段以了解是否已按下按钮吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:我正在尝试制作记事本应用程序.我的应用程序要做的是,按下一个按钮以创建新的笔记.这会弹出一个片段,用户可以在其中键入他的笔记.在同一片段中,我还有另一个按钮,用于指示用户何时完成输入.

问题1:是否可以通过一种方法来按下片段"中的另一个按钮来触发活动"中的某个方法?

问题2:这会导致该应用程序变得过于肿吗?我应该在活动本身中保留该按钮吗?

谢谢您的帮助.

解决方案

问题1:是否可以通过按下片段"中的另一个按钮来触发活动"中的方法?

当然,最简单的方法是:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    val binding = MyFragmentBinding.bind(view) // viewBinding enabled

    binding.myButton.setOnClickListener {
        (requireActivity() as MyActivity).doSomething() // <--
    }
}

但是,如果可以在不同的Activity实例中使用此Fragment,则它应该公开一个Listener,以暴露其潜在事件,而无需知道正在与之交谈的实际Activity实例.

interface ActionHandler {
    fun onMyButtonClicked()
}

lateinit var actionHandler: ActionHandler

override fun onAttach(context: Context) {
    super.onAttach(context)
    actionHandler = context as ActionHandler
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    val binding = MyFragmentBinding.bind(view) // viewBinding enabled

    binding.myButton.setOnClickListener {
        actionHandler.onMyButtonClicked()
    }
}

这样,即使在配置更改/进程终止后,您的Fragment仍将始终有一个侦听器与您交谈,这似乎不是此处大多数其他答案的情况.

问题2:这会导致该应用程序变得肿吗?我应该在活动本身中保留该按钮吗?

这取决于按钮是否实际上属于活动",尽管可能不属于该活动.无论如何,大多数现代应用都是以单活动形式编写的,除非视图在所有屏幕之间共享,否则将其放置在Fragment中,甚至可能使用来自公共布局资源的<include标签. >

The Objective: I'm trying to make a notepad application. What my app does is, a button is pressed to create a new note. This pops up a fragment in which the user types his note. Within the same fragment, I have another button that signifies when the user is done typing.

Question 1: Is there a way by which pressing the other button in the Fragment could trigger a method in my Activity?

Question 2: Would this cause the app to become too bloated? Should I keep the button within my activity itself?

Thank you for your help.

解决方案

Question 1: Is there a way by which pressing the other button in the Fragment could trigger a method in my Activity?

Sure, the simplest way to do it is:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    val binding = MyFragmentBinding.bind(view) // viewBinding enabled

    binding.myButton.setOnClickListener {
        (requireActivity() as MyActivity).doSomething() // <--
    }
}

However, if this Fragment can be used in different Activity instances, then it should expose a Listener with which it exposes its potential events, and doesn't need to know the actual Activity instance it is talking to.

interface ActionHandler {
    fun onMyButtonClicked()
}

lateinit var actionHandler: ActionHandler

override fun onAttach(context: Context) {
    super.onAttach(context)
    actionHandler = context as ActionHandler
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    val binding = MyFragmentBinding.bind(view) // viewBinding enabled

    binding.myButton.setOnClickListener {
        actionHandler.onMyButtonClicked()
    }
}

This way, your Fragment will always have a listener to talk to even after config changes / process death, which seems to not be the case for most other answers here.

Question 2: Would this cause the app to become too bloated? Should I keep the button within my activity itself?

This depends on whether the button actually belongs in the Activity, though it probably doesn't. Most modern apps are written as single-Activity anyway, and unless the view is shared among all screens, it's put inside a Fragment, possibly maybe even using <include tags from a common layout resource.

这篇关于活动可以访问片段以了解是否已按下按钮吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆