Keynes Dexter Ng
Keynes Dexter Ng

Reputation: 3

Buttons all open the same activity from an Android fragment

Currently I have a fragment_one.xml which has 5 CardViews on it and and each card has a Button on it that is meant to go to separate XML pages (Lesson_One,Lesson_Two etc...) but with the code I have in OneFragment.java, both buttons are opening Lesson_Two

How I can I fix this? Here is my code

FragmentOne.java

public class OneFragment extends Fragment{
   Intent intent;  

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
   }

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.fragment_one, container, false);
    intent = new Intent(getActivity(), LessonOne.class);
    final Button button = (Button) root.findViewById(R.id.button1);

    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            startActivity(intent);
        }
    });

    intent = new Intent(getActivity(), LessonTwo.class);
    final Button button2 = (Button) root.findViewById(R.id.button2);

    button2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            startActivity(intent);
        }
    });
    return root;
  }

}

Upvotes: 0

Views: 64

Answers (2)

OneCricketeer
OneCricketeer

Reputation: 191758

Alternative answer - implement the click listener on the class itself.

This cleans up the onCreateView method. You also don't need to "capture" the buttons to set their listeners.

public class OneFragment extends Fragment implements View.OnClickListener {

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

        root.findViewById(R.id.button1).setOnClickListener(this);
        root.findViewById(R.id.button2).setOnClickListener(this);

        return root;
    }

    @Override
    public void onClick(View v) {
        Class clz = null;
        switch (v.getId()) {
            case R.id.button1:
                clz = LessonOne.class;
            case R.id.button2;
                clz = LessonTwo.class;
        }

        if (clz != null) startActivity(new Intent(getActivity(), clz));

    }

}

Upvotes: 1

Kreempuff
Kreempuff

Reputation: 723

You're assigning intent twice, effectively overwriting the first intent with the second.

So no matter which click event is triggered LessonTwo.class is the activity that is started.

A simple fix would be to create the intents inside the click handlers like

public class OneFragment extends Fragment{


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.fragment_one, container, false);
    final Button button = (Button) root.findViewById(R.id.button1);

    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            startActivity(new Intent(getActivity(), LessonOne.class));
        }
    });

    final Button button2 = (Button) root.findViewById(R.id.button2);

    button2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            startActivity(new Intent(getActivity(), LessonTwo.class););
        }
    });

    return root;
  }

}

This makes it explicit which click handler starts what activity

Upvotes: 3

Related Questions