In my app I use DrawerLayout
. When I switch to MapFragment
that contains SupportMapFragment
and go back to it Google Map is initialized again. Is there any way to stop Google Map from doing it ? I'm using Kotlin. I have read about idea of using WeakReference to MapView but I don't understand how it may help in this case.
class MapFragment : Fragment(), OnMapReadyCallback {
lateinit var mMap: GoogleMap
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_map, container, false)
val mapFragment = childFragmentManager.findFragmentById((R.id.map_new)) as SupportMapFragment
mapFragment.getMapAsync(this)
return view
}
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
}
val navigationView: NavigationView = findViewById(R.id.nav_view)
navigationView.setNavigationItemSelectedListener {
if(it.itemId == R.id.nav_logout){
val intent = Intent(this, LoginActivity::class.java)
startActivity(intent)
true
}
else {
var fragment = when (it.itemId) {
R.id.nav_map -> MapFragment()
R.id.nav_forecast -> ForecastFragment()
R.id.nav_app_info -> AboutFragment()
else -> MapFragment()
}
fragmentManager
.beginTransaction()
.replace(R.id.main_activity_frame, fragment)
.commit()
it.isChecked = true
drawerLayout.closeDrawers()
true
}
}