Question

I have an issue. For analytic purposes I need to track when the APP (not activity) is resumed. The problem I have now is that if I put the tracker on the OnResume event of an activity, it will get fired every time the user goes back and forth on different activities.

How can I avoid that? How can I track the real "Application Resume," (when user actually exits the app and come back) and not the activity resume?

Any ideas is greatly appreciated. Thanks.

Was it helpful?

Solution

I encountered the same problem and solved it by creating base 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 the other activities in my app inherited mActivity. When an activity is no longer visible than onStop is called. when activities_num == 0 than all activities are not visible (meaning the the user close the app or it passed to the background). When the user start the application (or restarting it from the background) onStart will be called (onStart is called when the activity is visible) and activities_num > 0. hopes it helps...

OTHER TIPS

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

  1. Start tracking in the onCreate() of the Application object.
  2. Instrument all your Activities so their onPause() and onResume() methods check with the Application object and see if they are the first Activity to run, or if they are continuing a previously running instance of the app.
  3. Stop tracking in the onDestroy() of the Application object.

To a certain degree most of the analytics packages (Flurry and their ilk) do something similar to this. You'll need to do a little state machine work to get this to work right, but it shouldn't be too complicated.

Instead of OnResume(), hook into the OnCreate() event of your main activity.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top