How can I access getSupportFragmentManager() in a

2019-01-04 19:52发布

I have a FragmentActivity and I want to use a map fragment within it. I'm having a problem getting the support fragment manager to access it.

 if (googleMap == null) {
            googleMap = ((SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map1)).getMap();

            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }

            // create marker
            MarkerOptions marker = new MarkerOptions().position(
                    new LatLng(latitude, longitude)).title("Hello Maps ");

            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(new LatLng(latitude, longitude)).zoom(15).build();

            googleMap.animateCamera(CameraUpdateFactory
                    .newCameraPosition(cameraPosition));

            // adding marker
            googleMap.addMarker(marker);

13条回答
太酷不给撩
2楼-- · 2019-01-04 19:58

All you need to do is using

getFragmentManager()

method on your fragment. It will give you the support fragment manager, when you used it while adding this fragment.

Fragment Documentation

查看更多
等我变得足够好
3楼-- · 2019-01-04 20:00

You can directly call

getFragmentManager() 

to get the fragment manager.

or

In your fragment,

Create field :

private FragmentActivity myContext;

override onAttach method of your fragment :

@Override
public void onAttach(Activity activity) {
    myContext=(FragmentActivity) activity;
    super.onAttach(activity);
}

When you need to get Support fragment manager call :

FragmentManager fragManager = myContext.getSupportFragmentManager(); //If using fragments from support v4

or

FragmentManager fragManager = myContext.getFragmentManager();

You are done.

查看更多
走好不送
4楼-- · 2019-01-04 20:01

getActivity().getFragmentManager() This worked or me.

Also take a look at here.

看我几分像从前
5楼-- · 2019-01-04 20:01

Try this:

private SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getActivity().getSupportFragmentManager());
萌系小妹纸
6楼-- · 2019-01-04 20:04

You can use getActivity().getSupportFragmentManager() anytime you want to getSupportFragmentManager.

hierarchy is Activity -> fragment. fragment is not capable of directly calling getSupportFragmentManger but Activity can . Thus, you can use getActivity to call the current activity which the fragment is in and get getSupportFragmentManager()

Emotional °昔
7楼-- · 2019-01-04 20:04

My Parent Activity extends AppCompatActivity so I had to cast my context to AppCompatActivity instead of just Activity.

eg.

FragmentAddProduct fragmentAddProduct = FragmentAddProduct.newInstance();
FragmentTransaction fragmentTransaction = ((AppCompatActivity)mcontext).getSupportFragmentManager().beginTransaction();
登录 后发表回答