当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Can i put flurry code in onCreate() and onDestroy()?

问题描述:

I am trying to use flurry for my android app. It says that i should put flurry code in onStart() and onStop() methods. I dont have these methods in my code. I have two activities and both use onCreate() and onDestroy() methods only. Can i put flurry code in that? Will there be any problem with it?

网友答案:

onStart() and onStop() are methods that handle part of an activity lifecycle, so you can add them to your activities without any problem.

@Override
protected void onCreate(...) {
    super.onCreate(...);
            ...
}

@Override
protected void onStart() {
    super.onStart();
    FlurryAgent.onStartSession(this, "your_key");
}

@Override
public void onStop()
{
   super.onStop();
   FlurryAgent.onEndSession(this);
}
网友答案:

onStart and onStop are existing methods on an Activity, just like onCreate. If you want to add functionality at these points of the activity lifecycle, you can override them just like you did for onCreate.

public class MyActivity extends Activity {
    @Override
    protected void onStart() {
        super.onStart();
        FlurryAgent.onStartSession(this, "FLURRYKEY");
    }

    @Override
    public void onStop()
    {
       FlurryAgent.onEndSession(this);
       super.onStop();
    }
}

onCreate and onDestroy are not an appropriate pair of methods to use for Flurry session tracking because onDestroy is not guaranteed to be called. See the documention on onDestroy. You can end up with situations where the app gets killed by the system and Flurry will think the session is still going.

There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.

分享给朋友:
您可能感兴趣的文章:
随机阅读: