get the FragmentManager from within a Fragment (support.v4)

Be aware, I am using the I have searched all over the internet, I have found many similar questions, but they are all referring to and none of the solutions work with support.v4 FragmentManager.


I am using Fragments instead of Activities for navigation purposes. Inside my MainActivity I have a NavigationDrawer which is used to select a Fragment to be displayed using the following method:

public boolean onNavigationSelected(MenuItem item) {

    FragmentManager fragManager = getSupportFragmentManager();

    if(item.getItemId() == {
        fragManager.beginTransaction().replace(, new FragmentOne()).commit()
    if(item.getItemId() == {
        fragManager.beginTransaction().replace(, new FragmentTwo()).commit()


This works fine when a user navigates using the NavigationDrawer, but there are Buttons inside the Fragment which are used for navigation too, however the getSupportFragmentManager() method cannot be called from within a Fragment.


public class FragmentOne extends Fragment {

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        // Inflate the layout for the Fragment
        fragment = inflater.inflate(R.layout.fragment_one_layout, container, false);

        // Do some stuff to the fragment before returning it

        Button button = (Button) findViewById(;

        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                // This does not work
------------>   FragmentManager fragManager = getSupportFragmentManager();
                fragManager.beginTransaction().replace(, new FragmentTwo()).commit();

        return fragment;

1 answer

  • answered 2018-03-13 21:53 ianhanniballake

    The Activities' getSupportFragmentManager() is the Fragment's getFragmentManager().

    The only reason that the Activity's method is named getSupportFragmentManager() is to avoid conflicting with the framework getFragmentManager() method. There is no such requirement in the Fragment class.