프래그먼트에서 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());
'Programing' 카테고리의 다른 글
IntelliSense를 표시하는 Visual Studio 키보드 단축키 (0) | 2020.06.27 |
---|---|
기본적으로 AWS S3 버킷의 모든 객체를 공개하는 방법은 무엇입니까? (0) | 2020.06.27 |
필수 위조 방지 양식 필드 "__RequestVerificationToken"이 없습니다. 사용자 등록 오류 (0) | 2020.06.27 |
PHP를 사용하여 디렉토리의 전체 내용을 다른 디렉토리로 복사 (0) | 2020.06.27 |
이진 검색 복잡성을 계산하는 방법 (0) | 2020.06.27 |