0

I want a piece of code to be executed only when my app is launched or resumed. The problem is that I cannot use the onCreate() and onResume() methods because there is navigation between Activities within the app and the things I want to do must be done only when the app is launched or resumed.

Let's take an example, suppose there is an App which has Activities "A", "B" and "C" and user can navigate between these activities. I don't want to know when user has started activity "A", "B" or "C". What I want to know is when the user launched or resumed the "APP". Even clearer, if the main Activity is "A", let's imagine this navigation cycle Opens APP -> (1) Activity "A" -> (2) Activity "B" -> (3) Activity "A", then I would want this code to be executed only on (1).

Josemafuen
  • 682
  • 2
  • 16
  • 41
  • `I want a piece of code to be executed only when my app is launched or resumed.` This is exactly what `onCreate()` and `onResume()` are for. I don't understand why you cannot use these. – Code-Apprentice Aug 31 '16 at 22:43
  • `because there is navigation between Activities within the app` Can you provide more details about this? – Code-Apprentice Aug 31 '16 at 22:44
  • @JOSEMAFUEN : you can take advantage of onResume and onCreate() to know when your app was launched and resume just extent your class with ApplicationContext to know when your app launched and derive all your activities from one Parent activity to get to know when your app was resumed at central place. – dex Aug 31 '16 at 22:52
  • As CommonsWare has mentioned here: http://stackoverflow.com/questions/16046905/android-event-listener-for-app-lifecycle You can call registerActivityLifecycleCallbacks() on your Application object to listen for all Activity events – Michael Krause Aug 31 '16 at 22:58

0 Answers0