Reputation: 4587
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);
Upvotes: 217
Views: 330249
Reputation: 1403
in java you can replace with
getChildFragmentManager()
in kotlin,
childFragmentManager
Upvotes: 16
Reputation: 319
if you are using fragment manager in the activity
getSupportFragmentManager()
supportFragmentManager
if you are using fragment manager in the fragment
childFragmentManager
Upvotes: 3
Reputation: 59
I use this code and it's work:
val profileFragment = ProfileFragment()
val fragmentManager = parentFragmentManager
fragmentManager.beginTransaction().apply {
replace(R.id.fragment_main, profileFragment, ProfileFragment::class.java.simpleName)
addToBackStack(null)
commit()
the R.id.fragment_main is the main layout that you used. if you used main activity, then you can give id for the layout.
Upvotes: 0
Reputation: 14520
You can directly call
getParentFragmentManager()
to get the fragment manager. Note that getFragmentManager() also works but has been marked as deprecated.
Upvotes: 358
Reputation: 812
Best way to get fragmentManager inside of a fragment is to call the new method
getParentFragmentManager()
or use the property access syntax in kotlin files
parentFragmentManager
this specifies the fragmentManager that is to be loaded (choosing between child and parent fragments )
Upvotes: 1
Reputation: 1369
getFragmentManager()
has been deprecated in favor of getParentFragmentManager()
to make it clear that you want to access the fragment manager of the parent instead of any child fragments.
Simply use getParentFragmentManager()
in Java or parentFragmentManager
in Kotlin.
Upvotes: 9
Reputation: 875
The simple new way of doing it in kotlin
requireActivity().supportFragmentManager
Upvotes: 14
Reputation: 2013
Some Kotlin code to use supportFragmentManager inside a Fragment.
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
super.onCreateView(inflater, container, savedInstanceState)
val rootView = inflater.inflate(R.layout.fragment_home, container, false)
// ....
if (rootView.home_play_btn != null) {
rootView.home_play_btn.setOnClickListener {
val fragment: BaseFragment = ListFragment.newInstance()
val fragManager: FragmentManager = (activity as AppCompatActivity).supportFragmentManager
fragManager.beginTransaction()
.replace(R.id.content_home_frame, fragment, TAG).commit()
}
Upvotes: 1
Reputation: 1494
((AppCompatActivity) getActivity()).getSupportFragmentManager()
Upvotes: 4
Reputation: 2166
getActivity().getFragmentManager()
This worked or me.
Also take a look at here.
Upvotes: 3
Reputation: 967
The following code does the trick for me
SupportMapFragment mapFragment = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map));
mapFragment.getMapAsync(this);
Upvotes: 2
Reputation: 4822
if you have this problem and are on api level 21+ do this:
map = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map))
.getMap();
this will get the map when used inside of a fragment.
Upvotes: 16
Reputation: 18406
Kotlin users try this answer
(activity as AppCompatActivity).supportFragmentManager
Upvotes: 21
Reputation: 658
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();
Upvotes: 0
Reputation: 764
do this in your fragment
getActivity().getSupportFragmentManager()
Upvotes: 3
Reputation: 102
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();
Upvotes: 1
Reputation: 861
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();
Upvotes: 7
Reputation: 9234
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().
Upvotes: 56
Reputation: 2971
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()
Upvotes: 6
Reputation: 5566
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.
Upvotes: 85
Reputation: 4956
Try this:
private SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getActivity().getSupportFragmentManager());
Upvotes: 0