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 :)
You can use findFragmentByTag() or findFragmentById() functions to get a fragment. If mentioned methods are returning null then that fragment does not exist.
onStop() fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity. onDestroyView() allows the fragment to clean up resources associated with its View. onDestroy() called to do final cleanup of the fragment's state.
onStart()The onStart() method is called once the fragment gets visible. onResume()Fragment becomes active.
Adding and removing fragments. To add a fragment to a FragmentManager , call add() on the transaction. This method receives the ID of the container for the fragment, as well as the class name of the fragment you wish to add.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With