Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragments onClick method in fragment element

I read quite some articles about fragments, but I am still confused about how to do what.

I have a MainActivity, which displays two fragments side by side. In one of the fragments I have a button and defined in the fragments layout XML for the button

android:onClick="buttonClicked"

Now I want to implement that method

public void buttonClicked(View view)  

I would have assumed that this has to be implemented in FragmentA.java and not in MainActivity.java. But it only works if that method is implemented in MainActivity.java. Why is that? To me that doesn't make sense. Pre Honeycomb a method belonging to one activity stayed in that activity, now on a tablet I am merging many activities to one MainActivity and all the different methods are merged? Whatever do you put for example in FragmentA.java then? What if you have to start you an own activity because this app runs on a handheld, then the onClick method has not to be in the MainActivity but in the Activity which needs to be called then. I am pretty confused at the moment...

like image 660
AndyAndroid Avatar asked Dec 20 '11 15:12

AndyAndroid


People also ask

How can use click event in fragment in Android?

To implement View. OnClickListener in your Activity or Fragment, you have to override onClick method on your class. Firstly, link the button in xml layout to java by calling findViewById() method.

Can you pass the data between two fragments?

Share data between a parent and child fragment When working with child fragments, your parent fragment and its child fragments might need to share data with each other. To share data between these fragments, use the parent fragment as the ViewModel scope.

How do I open a button with a fragment?

You can replace the fragment using FragmentTransaction on button click. Something like this: Fragment someFragment = new SomeFragment(); FragmentTransaction transaction = getFragmentManager(). beginTransaction(); transaction.

What are dynamic fragments?

A fragment is usually used as part of an activity's user interface and contributes its own layout to the activity. A fragment is implemented as independent object -- independent of the activity that contains it. The benefit is that it can be used by multiple activities associated with the application.


1 Answers

I'm not sure what the specific problem is, but maybe this will help.

From the Android documentation on Fragments:

You should design each fragment as a modular and reusable activity component. That is, because each fragment defines its own layout and its own behavior with its own lifecycle callbacks, you can include one fragment in multiple activities, so you should design for reuse and avoid directly manipulating one fragment from another fragment.

That is, you should never manipulate a fragment from another fragment; rather, this should be done through the underlying Activity. Read the "Creating event callbacks to the activity" section in this article for more information (it's important stuff!!).

On the other hand, if you want the button to perform an action within the Fragment itself (i.e. if you wanted a Button click to change the text of a TextView within the Fragment), you should implement this in the Fragment, not the Activity (this is because the resulting behavior is contained within the Fragment and has nothing to do with the parent Activity).

Leave a comment and I can clarify if my post is confusing... I only recently began to understand Fragment's myself :).

like image 164
Alex Lockwood Avatar answered Sep 28 '22 08:09

Alex Lockwood