Android - How to track the application. Only resumes when you exit and return to the application?

I have a problem. For analytical purposes, I need to track when APP (inactivity) resumes. The problem that I now have in myself is that if I put the tracker in the OnResume activity of the activity, it will be launched every time the user starts different actions.

How can i avoid this? How can I keep track of the actual โ€œApplication Summaryโ€ (when the user really exits the application and returns), and not resume?

Any ideas are welcome. Thank you

+6
source share
3 answers

I ran into the same problem and solved it by creating basic activity:

public class mActivity extends Activity{ public static final String TAG = "mActivity"; public static int activities_num = 0; @Override protected void onStop() { super.onStop(); activities_num--; if(activities_num == 0){ Log.e(TAG,"user not longer in the application"); } } @Override protected void onStart() { super.onStart(); activities_num++; } } 

all other actions in my application are inherited. When activity is no longer displayed than onStop is called. when activity_num == 0, than all actions are not visible (this means that the user closes the application or transfers it to the background). When the user starts the application (or restarts it from the background), onStart will be called (onStart is called when activity is visible) and activity_num> 0. hopes this helps ...

+4
source

Use the application object of your application (see http://developer.android.com/reference/android/app/Application.html ). If you create your own application class and configure it in your AndroidManifest.xml file, you can do something like this:

  • Start tracking in onCreate() of the Application object.
  • Instrument all your actions so that their onPause() and onResume() methods check with the Application object and onResume() out if they are the first action launched or if they continue to use the previously launched instance of the application.
  • Stop tracking in onDestroy() of the Application object.

To some extent, most analytics packages (Flurry and their ilk) do something similar to this. You will need to do a little work with state machines to make it work correctly, but it should not be too difficult.

+2
source

Instead of OnResume (), connect to the OnCreate () event of your main action.

0
source

Source: https://habr.com/ru/post/887323/


All Articles