0

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?

Arno van Liere
  • 109
  • 1
  • 11
  • refer https://stackoverflow.com/questions/26265526/what-makes-my-map-fragment-loading-slow – sasikumar Apr 09 '19 at 13:11
  • the answer to that question only works if your mapview is not displayed right after your app launches as the answer mentions: 'My mapFragment is not displayed right after the app launches! Otherwise this would not make sense.' – Arno van Liere Apr 09 '19 at 16:04

0 Answers0