yojoannn
yojoannn

Reputation: 626

how to access button of fragment A from fragment B

I have two Fragments in my Activity: fragment A with button X and fragment B with button Y.

How can I change button X's background image when I click button B? Is it possible?

Upvotes: 3

Views: 5337

Answers (3)

windyzboy
windyzboy

Reputation: 1375

Base on Alex Lockwood's answer:

The activity:

public class SampleActivity extends Activity{

    public interface OnButtonClickedListener {
           public void onButtonClicked(); 
    }

    private OnButtonClickedListener onButtonClickedListener = null;

    public OnButtonClickedListener getOnButtonClickedListener () {
           return onButtonClickedListener 
    }

    public void setOnButtonClickedListener (
        OnButtonClickedListener onButtonClickedListener {
           this.onButtonClickedListener  = onButtonClickedListener;
    }

} 

Fragment A:

public class FragmentA extends Fragment {

private OnButtonClickedListener onButtonClickedListener = null;

private OnClickListener actionBarClickListener = new OnClickListener() {

    @Override
    public void onClick(View view) {
        if (onButtonClickedListener == null){
            onButtonClickedListener = ((SampleActivity) getActivity()).onButtonClickedListener ();
        }
        if (onButtonClickedListener != null) {
            onButtonClickedListener
                    .onButtonClicked();
        }
    }
};

}

Fragment B:

    public class FragmentB extends Fragment {

private OnButtonClickedListener onButtonClickedListener = new OnButtonClickedListener() {

    @Override
    public void onButtonClicked() {

        Toast.makeText(getActivity(), "Button clicked", Toast.LENGTH_SHORT).show();
    }
};

@Override
public void onResume() {
    super.onResume();
    SampleActivity sampleActivity = (SampleActivity) getActivity();
    sampleActivity.setSearchBoxTextChangedListener(onButtonClickedListener);
}

}

Hope can help someone.

Upvotes: 1

Diederik
Diederik

Reputation: 6488

Setting the onClick attribute for a button in your layout, even your fragment's layout, will call the appropriate method on your Activity.

Your app can then send this signal from your Activity to fragment B.

Upvotes: 0

Alex Lockwood
Alex Lockwood

Reputation: 83311

From the documentation,

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 being said, what you want to do is create event callbacks to the activity. A good way to do that is to define a callback interface inside the fragment and require that the host activity implement it. When the activity receives a callback through the interface, it can share the information with other fragments in the layout as necessary. This is the recommended way to share events between two separate Fragments--that is, sharing the event through the activity.

Check out the link above... it provides a couple nice examples. If you are still having trouble, let me know and maybe I can be more explicit.


Edit #1:

Let's say you click a button in fragment A and you want this to cause changes to a button in fragment B. Here's some sample code illustrating the concept:

The callback interface:

public interface OnButtonClickedListener {
    public void onButtonClicked(); 
}

The activity:

public class SampleActivity extends Activity implements OnButtonClickedListener {

    /* Implementation goes here */     

    public void onButtonClicked() {
        // This method is called from fragment A, and when it is called,
        //   it will send information to fragment B. Remember to first
        //   check to see if fragment B is non-null.

        /* Make call to a method in fragment B that will update its display */
    }
} 

Fragment A:

public class FragmentA extends Fragment {
    OnButtonClickedListener mListener;

    /* Implementation goes here */

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnButtonClickedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnButtonClickedListener ");
        }
    }

    public void clickButton() {
        // When the button is clicked, notify the activity.
        //   The activity will then pass the information to fragment
        //   B (if it has been created).

        mListener.onButtonClicked();
    }
}

Edit #2:

Now, you might be wondering, "Why would anyone ever go through all of this trouble? What's the point of creating a separate activity callback method when you could just have fragment A directly manipulate fragment B?"

The main reason you want to do this is to ensure that each fragment is designed as a modular and reusable activity component. This is especially important because a modular fragment allows you to change your fragment combinations for different screen sizes. When designing your application to support both tablets and handsets, you can reuse your fragments in different layout configurations to optimize the user experience based on the available screen space. For example, on a handset, it might be necessary to separate fragments to provide a single-pane UI when more than one cannot fit within the same activity. Making use of activity callbacks ensures that you will easily be able to reuse your fragments in situations where fragment B is not visible on the screen. For example, if you are on a handheld device and there is not enough room to display fragment B, then you can easily have your activity check to see if fragment B is currently being shown on the screen.

Sorry if this isn't clear... I'm finding it difficult to describe :P. Working your way through this tutorial might help... Activity callbacks make your life especially easier as a developer when you are working with interactive multi-pane layouts.

Upvotes: 11

Related Questions