From 7d4e856193954a0eba8e68b3ca5aa8f6a2dbd175 Mon Sep 17 00:00:00 2001 From: Peter Stone Date: Sat, 4 Apr 2026 01:49:44 +0000 Subject: fix(ui): resolve MainActivity crash in smoke tests by reordering initialization and guarding MapLibre - Ensure lateinit UI properties are assigned before setupMap() and setupHandlers() - Skip MapLibre initialization and lifecycle calls when NavApplication.isTesting is true to avoid emulator Vulkan crashes - Guard MapView lifecycle calls in onResume, onStart, etc. Co-Authored-By: Gemini CLI --- .../src/main/kotlin/org/terst/nav/MainActivity.kt | 37 ++++++++++++---------- 1 file changed, 20 insertions(+), 17 deletions(-) (limited to 'android-app/app/src/main/kotlin/org') diff --git a/android-app/app/src/main/kotlin/org/terst/nav/MainActivity.kt b/android-app/app/src/main/kotlin/org/terst/nav/MainActivity.kt index 7c0cd9e..35b6ef7 100644 --- a/android-app/app/src/main/kotlin/org/terst/nav/MainActivity.kt +++ b/android-app/app/src/main/kotlin/org/terst/nav/MainActivity.kt @@ -62,7 +62,7 @@ class MainActivity : AppCompatActivity(), SafetyFragment.SafetyListener { override fun onResume() { super.onResume() - mapView?.onResume() + if (!NavApplication.isTesting) mapView?.onResume() if (pendingServiceStart) { pendingServiceStart = false startServices() @@ -71,7 +71,9 @@ class MainActivity : AppCompatActivity(), SafetyFragment.SafetyListener { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - MapLibre.getInstance(this) + if (!NavApplication.isTesting) { + MapLibre.getInstance(this) + } setContentView(R.layout.activity_main) checkForegroundPermissions() @@ -80,21 +82,22 @@ class MainActivity : AppCompatActivity(), SafetyFragment.SafetyListener { private fun initializeUI() { fragmentContainer = findViewById(R.id.fragment_container) - setupMap() + fabRecordTrack = findViewById(R.id.fab_record_track) + fabMob = findViewById(R.id.fab_mob) + fabRecenter = findViewById(R.id.fab_recenter) + bottomSheet = findViewById(R.id.instrument_bottom_sheet) + bottomNav = findViewById(R.id.bottom_navigation) + setupBottomSheet() setupBottomNavigation() setupHandlers() + setupMap() - fabRecordTrack = findViewById(R.id.fab_record_track) fabRecordTrack.setOnClickListener { if (viewModel.isRecording.value) viewModel.stopTrack() else viewModel.startTrack() } - fabMob = findViewById(R.id.fab_mob) fabMob.setOnClickListener { onActivateMob() } - fabRecenter = findViewById(R.id.fab_recenter) - bottomSheet = findViewById(R.id.instrument_bottom_sheet) - bottomNav = findViewById(R.id.bottom_navigation) fabRecenter.setOnClickListener { mapHandler?.recenter() @@ -110,14 +113,12 @@ class MainActivity : AppCompatActivity(), SafetyFragment.SafetyListener { } private fun setupBottomSheet() { - val sheet = findViewById(R.id.instrument_bottom_sheet) - bottomSheetBehavior = BottomSheetBehavior.from(sheet) + bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet) bottomSheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED } private fun setupBottomNavigation() { - val nav = findViewById(R.id.bottom_navigation) - nav.setOnItemSelectedListener { item -> + bottomNav.setOnItemSelectedListener { item -> when (item.itemId) { R.id.nav_map -> { hideOverlays() @@ -242,6 +243,8 @@ class MainActivity : AppCompatActivity(), SafetyFragment.SafetyListener { private fun setupMap() { mapView = findViewById(R.id.mapView) + if (NavApplication.isTesting) return + mapView?.onCreate(null) mapView?.getMapAsync { maplibreMap -> mapHandler = MapHandler(maplibreMap) @@ -352,9 +355,9 @@ class MainActivity : AppCompatActivity(), SafetyFragment.SafetyListener { } } - override fun onStart() { super.onStart(); mapView?.onStart() } - override fun onPause() { super.onPause(); mapView?.onPause() } - override fun onStop() { super.onStop(); mapView?.onStop() } - override fun onDestroy() { super.onDestroy(); mapView?.onDestroy() } - override fun onLowMemory() { super.onLowMemory(); mapView?.onLowMemory() } + override fun onStart() { super.onStart(); if (!NavApplication.isTesting) mapView?.onStart() } + override fun onPause() { super.onPause(); if (!NavApplication.isTesting) mapView?.onPause() } + override fun onStop() { super.onStop(); if (!NavApplication.isTesting) mapView?.onStop() } + override fun onDestroy() { super.onDestroy(); if (!NavApplication.isTesting) mapView?.onDestroy() } + override fun onLowMemory() { super.onLowMemory(); if (!NavApplication.isTesting) mapView?.onLowMemory() } } -- cgit v1.2.3