iOS & Android onBackground & onForeground

Sometimes we want our App to perform some task when it is put to background (e.g. to save some data) or they are brought back to foreground (e.g. to sync some data).
Here I share how they are done on both iOS and Android, to know their similarity and differences.