-2

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

import android.support.v4.app.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() == R.id.fragment_one) {
        fragManager.beginTransaction().replace(R.id.content, new FragmentOne()).commit()
    }
    if(item.getItemId() == R.id.fragment_two) {
        fragManager.beginTransaction().replace(R.id.content, 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.

FragmentOne:

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(R.id.button);

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

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

        return fragment;
    }
}
Edwin Owen
  • 32
  • 4

1 Answers1

1

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.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • using `getFragmentManager()` I get the following error: Incompatible types. Required: android.support.v4.app.FragmentManager Found: android.app.FragmentManager – Edwin Owen Mar 13 '18 at 21:54
  • You still have to use `getSupportFragmentManager()` in the Activity. It is in the Fragment that you use `getFragmentManager`. Your error message doesn't come through in comments - try adding it to your question. – ianhanniballake Mar 13 '18 at 21:56
  • I pressed enter to go to next line, forgot to hold shift. I've edited previous comment with error message. – Edwin Owen Mar 13 '18 at 21:58
  • That's the error you get when you try to use `getFragmentManager()` in the Activity. That's not what you want to use. – ianhanniballake Mar 13 '18 at 22:06
  • Be sure that you are actually using the support version of Fragments. It could be the case that your are mixing SupportActivity with native Fragments. – mikehc Mar 13 '18 at 22:06
  • I am still using `getSupportFragmentManager()` in the Activity. I am getting the error inside the Fragment. – Edwin Owen Mar 13 '18 at 22:07
  • 100% sure I am using support Fragments. – Edwin Owen Mar 13 '18 at 22:08
  • 1
    Sounds like you're trying to use `getActivity().getFragmentManager()`? It is just `getFragmentManager()` in the Fragment. – ianhanniballake Mar 13 '18 at 22:14
  • Atmost you can have `getChildSupportFragmentManager()` within the fragments...anyway, you can solve your `button click-fragment transaction` using an interface !! @EdwinOwen – Santanu Sur Mar 14 '18 at 00:27