my game is still running when the user or turn off the screen or when you click the start button my idea is to put a component that when the user leaves the activity that is running do certain actions (I do not know if I understood)

Hi,
You can use extension taifun tools https://puravidaapps.com/tools.php
Possible values are pause , stop and resume in ActivityStateChanged
When You click home button on Your phone activityState have value pause. You can close app in that moment.
I am sending this message with phone, later I can sent You blocks.