4

I have an ImageButton I can access in my Activity and in my Fragment. I want actions to be done in both of those said classes so I implemented an onClickListener for both of them.

     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);

        ImageButton imageButton = (ImageButton) findViewById(R.id.my_image_button);
        imageButton.setOnClickListener(new OnClickListener() ...);
    }

And the same simple piece of code for the fragment launched by this activity (But in onViewCreated).

I tried it and only the Fragment's onClickListener is triggered.

So, here is my question, is there a way to make my button trigger both Activity's and Fragment's onClickListener ?

I know I could call a Fragment's method from the Activity's onClick, but it would be so much simpler to just trigger it inside the Fragment aswell.

EDIT :

I am not willing to do this through two OnClickListener at any cost, it was just in case there were a simpler way than Activity to Fragment callbacks (in my case).

As 空気嫁 said, a second onClickListener would disable the first one. In that case, only callbacks left.

Plus, after thinking a bit about it, it would make the code easier to understand too. Callbacks, yeah !

Cedric
  • 149
  • 1
  • 3
  • 12
  • 2
    *trigger both Activity's and Fragment's onClickListener ?* at the same time? – Tim Jul 12 '16 at 08:48
  • Yes. Two onClickListeners in different classes for one event. – Cedric Jul 12 '16 at 08:51
  • have you tried `getActivity().imageButton.setOnClickListener(new OnClickListener() ...);` (define `imageButton` as public) – Linh Jul 12 '16 at 08:51
  • 2
    **Is there a way to make my button trigger both Activity's and Fragment's onClickListener?** : No.. you can use interface callbacks instead..!! – Janki Gadhiya Jul 12 '16 at 08:55

6 Answers6

5

View.setOnClickListener only supports set one listener. If you call it twice, the later listener will cover the former listener and only the later listener will be notified. So call a Fragment's method from the Activity's onClick seems good for you. And if the ImageButton is defined in Activity's layout, it is good to deal with it only in Activity such as notifying others and exposing some methods.

空気嫁
  • 186
  • 5
3

I encountered a similar thing while working on a project a few weeks ago. I had a method in my fragment and one in my activity. To call them both, I put the button in the fragment, and in the click listener, I called these :

YourFragmentMethod(); //fragment actions
((YourActivity)getActivity()).yourActivityPublicMethod(); //activity actions

This way, you can trigger two actions with a single onClickListener, which seems a better option to me than two listeners on a single item.

YumeYume
  • 981
  • 2
  • 12
  • 33
1

Create on OnClickListner inerface like this

interface OnClickListner{
   void OnClick(View v);
}

Declare this interface as static in your activity

public static OnClickListner onClickListner;

Write a function which takes OnClickListner inteface as parameter in activity

public void setOnClickListner(OnClickListner onClickListner){
     this.onClickListner=onClickListner;
}

Add View.OnClickListner for your button in your activity as normal

imageButton.setOnClickListener(new OnClickListener() {
@override
onClick(View v){
  performActivitiesAction();
  //trigger fragment click listner
  if(onClickListner!=null){
    onClickListner.OnClick(v);
   }

});

In your fragment create object for your activity and apply setonclick listner

MyActivity myActivity=new MyActivity();
myActivity.setOnClickListner(new OnClickListner(){
   override
    OnClick(View v){
       //Write your fragment's click listner functions here
     }
 });
Jinesh Francis
  • 3,377
  • 3
  • 22
  • 37
0

In YourActivity

public ImageButton imageButton;
@Override
public void onCreate(Bundle savedInstanceState) {
    imageButton = (ImageButton) findViewById(R.id.my_image_button);
}

In your Fragment

((YourActivity) getActivity()).imageButton.setOnClickListener(new OnClickListener() ...);
Linh
  • 57,942
  • 23
  • 262
  • 279
0

You are not supposed to create 2 listeners for the same event in case of using fragment and activity.

Let's say your button exists inside the fragment layout, than you would set listener for that button inside your fragment code. After that you'll have to notice the activity about that click through interface.

Read more about communicating between activities and fragments here.

Community
  • 1
  • 1
Juvi
  • 1,078
  • 1
  • 19
  • 38
0

You should use only one listener and use it to do call the fragment as well. Make sure the fragment is not null and you should be ready to go:

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);

        ImageButton imageButton = (ImageButton) findViewById(R.id.my_image_button);
        imageButton.setOnClickListener(new OnClickListener(){
    // TODO: your actions in the activity
    // check if fragment is not null and call custom method
    if(mFragment != null){
        mFragment.onMainButtonClicked();
    }
});
    }
Björn Kechel
  • 7,933
  • 3
  • 54
  • 57
  • the way to get your fragment : `ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);` Then I agree, if not using 2 onClick, that's how I will do it. – Cedric Jul 12 '16 at 09:30