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

后端 未结 4 665
隐瞒了意图╮
隐瞒了意图╮ 2020-12-22 03:28

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 th

相关标签:
4条回答
  • 2020-12-22 03:53

    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.

    0 讨论(0)
  • 2020-12-22 03:58

    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));
        }
    
    0 讨论(0)
  • 2020-12-22 04:10

    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.

    0 讨论(0)
  • 2020-12-22 04:11

    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
        }
    
        ...
    }
    
    0 讨论(0)
提交回复
热议问题