I'm making an app with one activity in which all actions regarding drawer etc. are handeld, and three fragments in which the main content is displayed. In the MainActivity a fragment with a map is placed inside a fragmentcontainer. But when I start the app, the screen is white for 2-3 seconds before the map is showed.
I've tried with getMapAsync with an OnMapReadyCallback, which didn't work, and I've searched for other solutions, but couldn't find any that worked.
MainActivity.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
setSupportActionBar(toolbar)
supportActionBar?.title = getString(R.string.app_name)
val toggle = ActionBarDrawerToggle(
this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close
)
drawer_layout.addDrawerListener(toggle)
toggle.syncState()
nav_view.setNavigationItemSelectedListener(this)
supportFragmentManager.beginTransaction()
.replace(R.id.fragmentContainer, MapFragment())
.commit()
if (!checkDataConnection()) {
val dialogView = View.inflate(this, R.layout.dialog_no_data, null)
val dialogBuilder = AlertDialog.Builder(this)
.setView(dialogView)
.setCancelable(false)
val alertDialog = dialogBuilder.show()
dialogView.rescanDataConnection.setOnClickListener {
if (checkDataConnection()) {
alertDialog.dismiss()
}
}
}
}
MapFragment.kt
class MapFragment : Fragment(), OnMapReadyCallback {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_map, container, false)
val mapView = view.findViewById<MapView>(R.id.mapView)
mapView.onCreate(savedInstanceState)
mapView.getMapAsync(this)
try {
MapsInitializer.initialize(this.activity)
} catch (e: GooglePlayServicesNotAvailableException) {
e.printStackTrace()
}
return view
}
override fun onMapReady(p0: GoogleMap?) {
// Necessary function for loading map async
}
}
So can anybody help me with improving the performance?