MetroList: Prevent Double-unbind Crash in Service Lifecycle
Scritto da Alessio il 15/03/2026
Problem
Race condition in MainActivity lifecycle caused IllegalArgumentException when attempting to unbind a service that was already unbound or never bound. When onStop() calls unbindService() and onDestroy() also calls it under certain conditions, double-unbind occurs if the activity is destroyed before onStop() completes.
Solution
Added isServiceBound boolean flag to track binding state. Guard all unbindService() calls with the flag check. Extracted safeUnbindService() method for deterministic cleanup. Moved stopService() call outside the conditional unbind logic.
Result
No more crashes from double-unbind. Service cleanup happens deterministically regardless of activity lifecycle timing.
Commit: MetrolistGroup/Metrolist@a00968f
PR: #3254