In an Android app, a getString function running inside the onDestroy function of a service is returning the wrong string. Code:
override fun onDestroy() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
stopForeground(STOP_FOREGROUND_REMOVE)
} else {
stopForeground(true)
}
Log.i(TAG, "service destroyed")
sendBroadcast(Intent(ACTION_STOPPED))
StatusActivity.addMessage(getString(R.string.status_service_destroy))
}
The error occurred on the last line of the function. When debugging the right string is shown when using the Evaluate Expression function. This also occurred sporadically but repeatably in other locations in the code (such as fragments).
I tried to clean and rebuild. also tried to invalidate caches in Android Studio. The problem was solved by adding a Log.d function in the same file calling the getString function. The problem remained solved even after removing the log function.
I do not understand why.