Using intent to call a fragment from another fragment
You can't call your Fragment like this,
Intent intent = new Intent(view.getContext(), FragmentGreen.class);
view.getContext().startActivity(intent);
getActivity().finish();
you need to call your fragment like this way
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
FragmentGreen llf = new FragmentGreen();
ft.replace(R.id.listFragment, llf);
ft.commit();
Intent
is basically used for call one activity from another. For add new Fragment
you can't use Intent
. For that you have to use FragmentManager
and for open fragment FragmentTransaction
.
for more details go Here
If all the fragments are within the same ViewPager and you just want to be navigated to that fragment then you dont need any Intents
. all you need to is :
mViewPager.setCurrentItem(POSITION, true);
here POSITION is an integer.
Now the view pager is in Activity
but the Button
is inside Fragment
so you need to do this create this method inside your activity
public void selectFragment(int position){
mViewPager.setCurrentItem(position, true);
// true is to animate the transaction
}
and call like this inside your OnClickListener
:
((MainActivity)getActivity()).selectFragment(POSITION_YOU_WANNA_SELECT);
Hope it helps.