1

Lets say I have a fragment MyFragment and I create myFragment1 and myFragment2. Both share the same xml layout which is just a image button. Using onClick, how can I make it so that clicking either button doesn't do the same thing?

For example, if I want myFragment1's button to go to Activity A, and I want myFragment2's button to go to Activity B.

Sample code below:

public class MyFragment extends Fragment implements View.OnClickListener{

    ImageButton myButton;
    View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_device, container, false);

        myButton = (ImageButton) view.findViewById(R.id.myButton);
        myButton.setOnClickListener(this);

        return view;
    }


    @Override
    public void onClick(View v){
        //do something
    }
}

From my MainActivity:

public class MainActivity extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        LinearLayout windowForMainActivity = (LinearLayout) findViewById(R.id.windowForMainActivity);

        MyFragment myFragment1 = new MyFragment();
        fragmentTransaction.add(windowForMainActivity.getId(),myFragment1);

        MyFragment myFragment2 = new MyFragment();
        fragmentTransaction.add(windowForMainActivity.getId(),myFragment2);

        fragmentTransaction.commit();
    }
}
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
beleven
  • 23
  • 5

2 Answers2

1

First from your activity send data to fragment like this

Bundle bundle = new Bundle();
bundle.putString("some_value", "ActivityA");
MyFragment myFragment1 = new MyFragment();
myFragment1.setArguments(bundle);
fragmentTransaction.add(windowForMainActivity.getId(),myFragment1);

Bundle bundle2 = new Bundle();
bundle2.putString("some_value", "ActivityB");
MyFragment myFragment2 = new MyFragment();
myFragment2.setArguments(bundle);
fragmentTransaction.add(windowForMainActivity.getId(),myFragment2);

And then get the value like this

String whichActivity;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    whichActivity = getArguments().getString("some_value");    
    return inflater.inflate(R.layout.fragment, container, false);
}

@Override
    public void onClick(View v){
        if(whichActivity.equals("ActivityA")){
          //code to start Activity A
        }else{
         //code to start activity B
        }
}
Mithun Sarker Shuvro
  • 3,902
  • 6
  • 32
  • 64
0

Since you've made the class implement the interface, when you create the Fragment, you can override the onClick.

MyFragment frag1 = new MyFragment() {
    @Override
    public void onClick(View v) {
        Intent i = new Intent(MainActivity.this, ActivityA.class);
        MainActivity.this.startActivity(i);
    }
};

MyFragment frag2 = new MyFragment() {
    @Override
    public void onClick(View v) {
        Intent i = new Intent(MainActivity.this, ActivityB.class);
        MainActivity.this.startActivity(i);
    }
};

Of course, there could be better ways to do that within the Fragment class itself using Bundle arguments, for example.

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245