Java – number of clicks to hear the activity button from within the clip
I just started learning fragments. I have an activity, that is, 1000 lines of code, which I have converted into fragments
I wonder if there is any simple way to allow my fragment to listen for buttons residing in the XML of its parent activity I tried to add a regular listener in the oncreateview of the fragment, which contains the ID of the button, but it (expected) will give me a null pointer exception
I know the onsearchclicklistener method to do this, but it may take a lot of time to manually refactor to achieve my needs (at least from the usage I can say, I'm glad to make mistakes), so I wonder if anyone knows any other method to do this in a non repetitive way
Solution
You can try to get the View reference in the activity in this way:
View view = getActivity().findViewById(R.id.view_in_activity);
Then add a listener:
view.setOnClickListener(new OnClickListener() { void onClick(View v) { // Perform some action } })