Track when the user opens and closes the app

I need to track when the user opens and closes the app and then log both instances with the time. How can I achieve this?

Requirements
1- clock component
2- tinydb component

when user close application

enable clock with 1000 ms(1 sec) on screen intialize and then in clock timer event
save current time in tiny db by tag eg…close app

For getting last closed time of app

then get that tinytb tag on Screen1 initialize that gives you last closed time of app

You can use an activity (or app) lifecycle extension and database of your choice for this purpose.

Alternatively, you can have analytics extension.