Your data. Anywhere you go.

New Relic for iOS or Android


Download on the App Store    Android App on Google play


New Relic Insights App for iOS


Download on the App Store


Learn more

Close icon

Relic Solution: Android initialization and deep linking

android

#1

It’s important to place the New Relic Android agent start call within the default activity (usually MainActivity) of an application. Similarly, starting the agent in another class - such as the application class - is not supported and can cause unexpected or unstable behavior.

This is fine for most apps, but what if an application utilizes deep linking - meaning there are essentially multiple default activities which can be launched when the app is started?

I’ll walk through two possible configuration options to ensure the agent is properly started no matter how deep the linking goes.

Easy Mode

Likely the most straightforward solution is to simply place the agent start call within the onCreate() method of multiple activities - which can be launched through linking. We’ve baked in validation which will ensure the agent isn’t being started multiple times.

Multiple start call example.

The standard agent initialization - within the MainActivity:

public class MainActivity extends AppCompatActivity {
   ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        NewRelic.withApplicationToken("<App_Token_Here>").start(this.getApplication());
        ...
    }  
}

The agent initialized within an activity - which can be launched through deep linking:

public class LinkedActivity extends AppCompatActivity {
   ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        NewRelic.withApplicationToken("<App_Token_Here>").start(this.getApplication());
        ...
    }
}

Hero Mode

Now let’s tackle an option which might be more efficient if an application extensively utilizes linked activities. We can create a custom base activity which initializes the agent - and is then extended by each activity.

Base Activity example.

Initializing within the CustomBaseActivity will look familiar:

public class CustomBaseActivity extends AppCompatActivity {
   ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        NewRelic.withApplicationToken("<App_Token_Here>").start(this.getApplication());
        ...
    }
}

Then you can extend this CustomBaseActivity in other activities, like so:

public class LinkedActivity extends CustomBaseActivity {
   ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
    }
}

If you start seeing interactions being recorded as the base activity (like CustomBaseActivity) - please see our Relic Solution about interaction naming for more information on this behavior.

Now you can deep link like you’re in the ninth dungeon! (yes, that’s a Zelda joke)


Relic Solution: Why Am I Seeing Inflated App Launch Count In My Android App?