Programing

프래그먼트에서 getSupportFragmentManager ()에 어떻게 액세스 할 수 있습니까?

lottogame 2020. 6. 27. 10:57
반응형

프래그먼트에서 getSupportFragmentManager ()에 어떻게 액세스 할 수 있습니까?


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

직접 전화 할 수 있습니다

getFragmentManager() 

조각 관리자를 얻을 수 있습니다.

또는

조각에서

필드 만들기 :

private FragmentActivity myContext;

프래그먼트의 onAttach 메소드를 재정의하십시오.

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

Support fragment manager call을 받아야 할 경우 :

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

또는

FragmentManager fragManager = myContext.getFragmentManager();

끝났습니다.


당신이해야 할 모든 사용

getFragmentManager()

당신의 조각에 방법. 이 프래그먼트를 추가하는 동안 사용했을 때 지원 프래그먼트 관리자를 제공합니다.

조각 문서


간단히 다음과 같이하십시오-

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.

또는

getActivity().getSupportFragmentManager();

Fragment의 onActivityCreated () 메소드와 onActivityCreated () 이후에 호출되는 모든 메소드에서.


Kotlin 사용자는이 답변을 시도

(activity as AppCompatActivity).supportFragmentManager

이 문제가 있고 API 레벨 21 이상인 경우 다음을 수행하십시오.

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

이것은 조각 내부에서 사용될 때 맵을 얻습니다.


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


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

do this in your fragment

getActivity().getSupportFragmentManager()

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

Also take a look at here.


The following code does the trick for me

 SupportMapFragment mapFragment = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map));
    mapFragment.getMapAsync(this);

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

You can simply access like

Context mContext;

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

mContext = getActivity();

}

and then use

FragmentManager fm = ((FragmentActivity) mContext)
                        .getSupportFragmentManager();

getSupportFragmentManager() used when you are in activity and want to get a fragment but in the fragment you can access

getSupportFragmentManager()

by use another method called getFragmentMangaer() works the same like getSupportFragmentManager() and you can use it like you used to:

fragmentTransaction =getFragmentManager().beginTransaction();

You can use childFragmentManager inside Fragments.


Try this:

private SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getActivity().getSupportFragmentManager());

참고URL : https://stackoverflow.com/questions/20237531/how-can-i-access-getsupportfragmentmanager-in-a-fragment

반응형