In my application I have multiple fragments, but I am not been able to switch between the fragments. When I click button1, it display the correct fragment. But when I click button2, it does not display the corresponding fragment.
Following is my relevant code:
fragmentM = getFragmentManager();
transation = fragmentM.beginTransaction();
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.search:
break;
case R.id.btnun:
detail = new DetailFragment();
transation.add(R.id.fragment_container, detail);
transation.addToBackStack(null);
transation.commit();
break;
case R.id.btnch:
detailt = new DetailtFragment();
transation.replace(R.id.fragment_container, detailt);
transation.addToBackStack(null);
transation.commit();
break;
}
They appear to be different classes of Fragment so I think that’s ok.
You should try and move the getFragmentManager.beginTransaction() lines to the start of the onClick method. It is my understanding that you need to get a new transaction every time you want to make a change.