Android 如何在片段中访问 getSupportFragmentManager()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20237531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:30:45  来源:igfitidea点击:

How can I access getSupportFragmentManager() in a fragment?

androidandroid-fragmentsandroid-support-libraryandroid-fragmentactivityfragmentmanager

提问by DjP

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.

我有一个 FragmentActivity,我想在其中使用一个地图片段。我在让支持片段管理器访问它时遇到问题。

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

回答by Eldhose M Babu

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 :

覆盖片段的 onAttach 方法:

@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.

你完成了。

回答by Mark

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

片段文档

回答by Varundroid

Simply get it like this -

简单地得到它这样 -

getFragmentManager() // This will also give you the SupportFragmentManager or FragmentManager based on which Fragment class you have extended - android.support.v4.app.Fragment OR android.app.Fragment.

OR

或者

getActivity().getSupportFragmentManager();

in your Fragment's onActivityCreated() method and any method that is being called after onActivityCreated().

在您的 Fragment 的 onActivityCreated() 方法以及在 onActivityCreated() 之后调用的任何方法中。

回答by Ranjith Kumar

Kotlin users try this answer

Kotlin 用户试试这个答案

(activity as AppCompatActivity).supportFragmentManager

回答by bsautner

if you have this problem and are on api level 21+ do this:

如果您遇到此问题并且处于 api 级别 21+,请执行以下操作:

   map = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map))
                    .getMap();

this will get the map when used inside of a fragment.

这将在片段内部使用时获得地图。

回答by Emil Re?a Enriquez

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

您可以getActivity().getSupportFragmentManager()随时使用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()

层次结构是活动-> 片段。fragment 不能直接调用 getSupportFragmentManger 但 Activity 可以。因此,您可以使用 getActivity 调用片段所在的当前活动并获取getSupportFragmentManager()

回答by Akinola Olayinka

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

我的父活动扩展了 AppCompatActivity,所以我不得不将我的上下文转换为 AppCompatActivity 而不仅仅是 Activity。

eg.

例如。

FragmentAddProduct fragmentAddProduct = FragmentAddProduct.newInstance();
FragmentTransaction fragmentTransaction = ((AppCompatActivity)mcontext).getSupportFragmentManager().beginTransaction();

回答by philip mudenyo

do this in your fragment

在您的片段中执行此操作

getActivity().getSupportFragmentManager()

回答by Aravindh Gopi

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

getActivity().getFragmentManager()这工作或我。

Also take a look at here.

也看看这里。

回答by Diako Hasani

((AppCompatActivity) getActivity()).getSupportFragmentManager()