简体   繁体   中英

Reliable way to detect application launch from home / desktop?

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

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM