Tabbed activity(fragment) to activity navigation using onClick

721 views Asked by At

I have a Tabbed activity with 3 tabs and each tab has its own fragment. there's a button inside the first tab which i want to click and navigate to another Tabbed activity (or any activity which has a fragment). How do I accomplish this ? I am trying to click on a button and open a new activity but i am unable to do so. I would really appreciate if i get some headers as I'm learning android. Here's a link to my fragment class for reference.

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

    Button button1 = (Button) view.findViewById(R.id.introbtn1);
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent tutorial = new Intent(getActivity(), TutorialIntroduction.class);
            startActivity(tutorial);

            //Tried to create a toast to check if the button works but it doesn't
            //Toast.makeText(getActivity(), "button is clicked!", Toast.LENGTH_LONG).show();
        }
    });

    return view;
}
1

There are 1 answers

0
nebuchadnezzar I On

In your fragment create a ViewPager variable and a setter method for it that is called when the fragment is created in your activity(its better to do this in constructor but it says you require it empty). Then in the onClick

ViewPager.setCurrentItem(FragmentPostion)

FragmentPostion is the int of which page you want to switch to, in your case from 0-2 (or 1-3 can't remember, try both haha).