Can an Activity access a Fragment to know if a button has been pressed?

淺唱寂寞╮ 提交于 2020-06-09 04:20:07

问题


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.


回答1:


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.




回答2:


There is an easy way of doing this as your fragments have access to activity (Kotlin) | getActivity() (Java) and by casting it you can use it. But this is not the proper way of doing this because it affects the modularity of fragments.

The proper way of doing this:

Your activity wants to listen to Fragments events without any overhead:

In Fragment

class MyFragment : Fragment() {

    override fun onAttach(context: Context) {
        super.onAttach(context)
        if (context is MyFragment.Listener) {
            listener = context
        } else {
            throw ClassCastException(context.toString() + " You need to implement MyFragment.Listener")
        }
    }


    interface Listener {
        fun onSomethingHappened()
    }

    private var listener: MyFragment.Listener? = null

    fun aMethodInsideFragmentThatHandlesButtonEvents() {
        listener?.onSomethingHappened()
    }
}

And in your activity:

class MyActivity : AppCompatActivity(), MyFragment.Listener {
    override void onSomethingHappened() {
        // do your work here
    }

    ...
}



回答3:


For triggering a method on click of a button in fragment, there are number of ways to achieve this. Try this.

If (getActivity() instanceof MainActivity){
     //Getting instance of your activity
     MainActivity instance = ((MainActivity)getActivity());
     //Using the instance calling the method in your activity
     instance.methodName();
}

Use the above code in your fragment on button click.

Another way is using Interface, calling its abstract methods in fragment and overriding it MainActivity; on button click those methods will be called.

Or you can also try using RxEventBus. You can publish it in the fragment and listen in the MainActivity.

Hope this resolves your issue.




回答4:


Just make your activity implement View.OnClickListener and on your fragment set your activity as onClickListener of your button.

your fragment:

   @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        myButton.setOnclickListener((MyActivity)getActivity));
    }


来源:https://stackoverflow.com/questions/62126408/can-an-activity-access-a-fragment-to-know-if-a-button-has-been-pressed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!