Trigger function upon app exit

So I’m looking for 3 scenarios for my application…

  1. When the user is in the app everything works (obviously)

  2. When the app is either quit or put in the background (user presses home button) I want to trigger Function A

  3. When the user is in the app and simply locks there phone (doesn’t exit the app or anything) , I want to trigger Function C.

Can someone help me differentiate the 3 scenarios above?