Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :)

like image 245
CENT1PEDE Avatar asked Nov 18 '13 12:11

CENT1PEDE


People also ask

How do you check fragment is added or not?

You can use findFragmentByTag() or findFragmentById() functions to get a fragment. If mentioned methods are returning null then that fragment does not exist.

What fragment method is called when the fragment is no longer visible to the user?

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.

Which method is called once the fragments gets visible?

onStart()The onStart() method is called once the fragment gets visible. onResume()Fragment becomes active.

How add and remove fragments?

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.


1 Answers

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.

like image 102
Rajesh Batth Avatar answered Oct 19 '22 23:10

Rajesh Batth