I've got a collections of fragment as : [Fragment1][Fragment2][Fragment3]
I'm using ViewPager and FragmentPagerAdapter. What I'm trying to do is to replace Fragment2 to another fragment (let's call it Fragment4), when a view inside Fragment2 is clicked. I'm using the code as below :
newMsgBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
swapFragment(new Fragment4());
}
});
private void swapFragment(Fragment fragment){
FragmentTransaction fragmentTransaction = ((FragmentActivity)getContext()).getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.view_container,fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
Then when I clicked on the newMsgBtn, it detect the click but it's unable to change the fragment. Any idea how to do this ? thanks