The Green Foxx The Green Foxx - 5 months ago 27
Android Question

Fragment not showing when add() method is called

here is the declarations:

FragmentManager fr = getFragmentManager();
FragmentTransaction ft = fr.beginTransaction();
Fragment myFragment = new defaultFragment();
ft.add(R.id.fragment, myFragment);
ft.commit();


and here is the
onClick()
method:

public void startFragment(View v){
newFragment = new nextFragment();
execute();
}


execute method :

public void execute() {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.add(R.id.fragment, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}


i cant the problem but when i click the button that triggers the
startFragment(View v)
method, nothing happens, the fragment is not replaced by the fragment that i want to add on the stack. I think the problem has something to do with this line
transaction.add(R.id.fragment, newFragment);
help please thanks :)

Answer

Try adding the fragment to FrameLayout instead. I was not able to add it to LinearLayout, but I changed it to FrameLayout and it worked.

Comments