Map Fragment
This example demonstrates how to load a map within an Android fragment using the Mapbox Maps SDK for Android.
The MapFragment
in this example loads a satellite map style from Mapbox. The activity handles the addition of new fragments by replacing the current fragment with a new MapFragment
instance, while managing the fragment back stack with unique identifiers. Additionally, a custom EmptyFragment
class is included, displaying a text view within the fragment.
This example code is part of the Maps SDK for Android Examples App, a working Android project available on GitHub. Android developers are encouraged to run the examples app locally to interact with this example in an emulator and explore other features of the Maps SDK.
See our Run the Maps SDK for Android Examples App tutorial for step-by-step instructions.
package com.mapbox.maps.testapp.examples
import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.mapbox.maps.MapboxMap
import com.mapbox.maps.Style
import com.mapbox.maps.testapp.R
import com.mapbox.maps.testapp.databinding.ActivityEmptyFabBinding
import com.mapbox.maps.testapp.examples.fragment.MapFragment
class FragmentBackStackActivity : AppCompatActivity() {
private lateinit var mapFragment: MapFragment
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivityEmptyFabBinding.inflate(layoutInflater)
setContentView(binding.root)
if (savedInstanceState == null) {
mapFragment = MapFragment()
mapFragment.getMapAsync {
initMap(it)
}
supportFragmentManager.beginTransaction().apply {
add(R.id.container, mapFragment, FRAGMENT_TAG)
}.commit()
} else {
supportFragmentManager.findFragmentByTag(FRAGMENT_TAG)?.also { fragment ->
if (fragment is MapFragment) {
fragment.getMapAsync {
initMap(it)
}
}
}
}
binding.displayOnSecondDisplayButton.setOnClickListener { handleClick() }
binding.fragmentButton.setOnClickListener { addNewFragment() }
}
private fun addNewFragment() {
supportFragmentManager.beginTransaction().apply {
replace(
R.id.container,
MapFragment().also {
it.getMapAsync { mapboxMap -> initMap(mapboxMap) }
}
)
addToBackStack("map_new_fragment_${System.currentTimeMillis()}")
}.commit()
}
private fun initMap(mapboxMap: MapboxMap) {
mapboxMap.loadStyle(Style.SATELLITE)
}
private fun handleClick() {
supportFragmentManager.beginTransaction().apply {
replace(R.id.container, EmptyFragment.newInstance())
addToBackStack("map_empty_fragment")
}.commit()
}
class EmptyFragment : androidx.fragment.app.Fragment() {
companion object {
fun newInstance(): EmptyFragment {
return EmptyFragment()
}
}
@SuppressLint("SetTextI18n")
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val textView = TextView(inflater.context)
textView.text = "This is an empty Fragment"
return textView
}
}
companion object {
private const val FRAGMENT_TAG = "map_fragment"
}
}