1

I want to detect each time the user opens the app, by tap on home / desktop icon. There seem not to be a straight forward way to do it. Found a few workarounds but nothing seems to be really reliable.

Things like, extend application object and use method "onCreate()", but this is not what I need because it's not called always when the user taps on the app's icon (can be just brought from the background, launching doesn't necessarily recreate the application), and also the application may be destroyed and recreated while running. Then Application.onCreate() will also be called.

There war also some approaches involving BroadcastReceiver and checking intent flags but everything seems to be also not quite reliable?

I need this because I want to track with Google Analytics, when the user opens the app.

Thanks

User
  • 31,811
  • 40
  • 131
  • 232

2 Answers2

0

Try to look into "android application lifecycle".

But onResume is launched every time you start your activity

Else try : onStart which is called every time your application has been sent to "background". It really does state so in the developer docs.

Anders Metnik
  • 6,096
  • 7
  • 40
  • 79
  • So? I know the lifecycle and there's nothing to detect each time the app is launched. `onResume` like you say is called each time an activity is started, and also when something in the foreground from the app goes away etc. so it's not suitable. – User Aug 22 '12 at 11:54
  • Well you can't have it both ways, the nearest thing is onStart then. it's a middle way. look at the link I posted to @Zoombie' answer – Anders Metnik Aug 22 '12 at 11:57
  • onStart is also called when the activity was called explicitly from inside the app, or when it's being recreated after destroy, and, as you say, when the application is in the background/behind some other activity. When the other activity is closed, and the app becomes visible again (without the user having launched it explicitly), onStart() is called. – User Aug 22 '12 at 12:28
  • But there is no other way then, other then creating a specific broadcast receiver for the launch intents then. And I ain't even sure that will work either. – Anders Metnik Aug 23 '12 at 08:54
0

Whenever your application is launched by normal way [if user taps on icon on home launcher] then main Activity for which

<activity android:name=".xyz" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>

is defined, in this application onCreate method will surely be called. So here you can put you Google Analaytics tracking code.

In other ways, like broadcast receiver it really depends upon which activity is called upon and whether it is start of application. There too you can put in onReceive method

As far as i feel, Android has definate way of start an app, and its always reliable. Only lifecycle of Android are bit tricky.

Zoombie
  • 3,590
  • 5
  • 33
  • 40
  • onCreate is not called if you havn't closed it/android os havnt killed it. It may simply be lying in the background. http://developer.android.com/reference/android/app/Activity.html – Anders Metnik Aug 22 '12 at 11:54
  • And onCreate is also called if I start this activity explicitly from inside the app or the app is destroyed and re-created. These are cases I don't want to track. – User Aug 22 '12 at 11:56
  • if application background and foreground is case, then it has different lifecycle methods, in above case onRestart and then onResume will be called sequentially. But question is only for reliable way to launch an app from Home, it doesn't say anything about app being in foreground or background – Zoombie Aug 22 '12 at 11:59
  • What Anders Metnik is saying is that onCreate will not be called if the user launches it, from the desktop, but the app is only in the background. So it's not useful to detect launch. onStart is called usually, when this activity is launched from inside the app itself, also onResume. So they are also not useful to detect launch. – User Aug 22 '12 at 12:05