Search code examples
androidandroid-fragmentactivity

Fragments - How to


I have three fragments in two activities;fragmentA in activityA and fragmentB,fragmentC in activityB.when i click a button in fragmentA i want to go specifically to fragmentB (not fragment c) in activityB. please help me in this regard.


Solution

  • add this in OnCreateView of Fragment A:

    Button button = v.findViewById(R.id.mybutton);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), ActivityB.class);
            startActivity(intent);
        }
    });