I know this happens when the app is restricted in battery optimization settings, but I picked the option for the system not to close the app for battery saving purposes (on Samsung phones, the option is called “unrestricted”). Still, the app often randomly closes when music is not playing, and that resets the flow, or the last played song.
This does not happen when music is playing, but it can happen within moments after stopping the music, as long as I am not in the app itself.