views:

46

answers:

1

I'm trying to integrate Google Analytics into my Android project using the information on the website of the respective SDK. However, there is very little documentation available. My project has 6 different Activities and I noticed that using the method on the website results in a unique visit in Google Analytics for each Activity that is being opened, even if it's still in the same session. Apparently, Google Analytics for Android never reuses a previously used session.

Their method is to start tracking activity in onCreate and then stop tracking in onDestroy. The problem I have with this is that a session will stay active if a user presses the home button instead of the back button, since the Activity will not be destroyed. Therefore I chose to do it in onResume and onPause instead but that means that new sessions gets opened when a new Activity is opened.

Does anyone know any way to really track a single session over multiple Activities?

A: 

After studying the lifecycle of an Activity, I came to the following conclusion.

When switching from an Activity A to another Activity B, the onStop method of A is called AFTER the onStart method of B. What I then did was increasing a reference counter every time the (static) tracker is accessed in an onStart method. In the onStop method, I would first check whether the reference counter was 0, and stop the tracker if it was. At the end of the onStop method, I would decrease the reference counter.

This seems to be working quite well at this moment, and should also work when the application has multiple Activities that can act as an entry point.

Franklin
I noticed one downside: onStop doesn't get called when the screen of the device is turned off so the session will stay active ...
Franklin