Flurry analytics in every Activity?

泄露秘密 提交于 2019-12-05 11:46:22
florianmski

Here is a great answer : https://stackoverflow.com/a/8062568/1635817

I suggest you to create a "BaseActivity" and to tell all your activities to extend it so you don't have to copy/paste those lines in every activity class.

Something like this :

public class BaseActivity extends Activity
{
    public void onStart()
    {
       super.onStart();
       FlurryAgent.onStartSession(this, "YOUR_KEY");
       // your code
    }

    public void onStop()
    {
       super.onStop();
       FlurryAgent.onEndSession(this);
       // your code
    }
}

In response to @conor comment :

From Flurry's documentation

So long as there is any Context that has called onStartSession(Context, String) but not onEndSession(Context), the session will be continued. Also, if a new Context calls onStartSession(Context, String) within 10 seconds (the default session timeout length) of the last Context calling onEndSession, then the session will be resumed, instead of a new session being created. Session length, usage frequency, events and errors will continue to be tracked as part of the same session. This ensures that as a user transitions from one Activity to another in your application they will not have a separate session tracked for each Activity, but will have a single session that spans many activities.

Andrey Mischenko

Answer from florianmski has sense, but there are some problems when you have to use different kinds of activities in your application such as FragmentActivity, TabActivity, ListActivity and so on. In this case you are not able to extend all your activities from single BaseActivity. Personally I would prefer to put calls of onStartSession and onEndSession in each activity's onStart and onStop methods, but before wrap them into some class, for example:

public class Analytics {
    public static void startSession(Context context) {
        FlurryAgent.onStartSession(context, Config.FLURRY_KEY);
        // here could be some other analytics calls (google analytics, etc)
    }
    public static void stopSession(Context context) {
        FlurryAgent.onEndSession(context);
        // other analytics calls
    }
}

Inside each activity:

public void onStart() {
    super.onStart();
    Analytics.startSession(this);
}

public void onStop() {
    super.onStop()
    Analytics.stopSession(this);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!