Fragment

The sentry-android-fragment library provides Fragment support for Sentry using the FragmentLifecycleIntegration. The source can be found on GitHub.

On this page, we get you up and running with Sentry's Fragment Integration, so that it will automatically add a breadcrumb for each fragment's lifecycle and start a span out of the active transaction bound to the scope for each launch of a fragment.

Auto-Installation With the Sentry Android Gradle Plugin

Install

Starting from version 3.1.0, the Sentry Android Gradle plugin will automatically add the sentry-android-fragment dependency. The plugin will only add the sentry-android-fragment dependency if a androidx.fragment dependency was discovered on the classpath.

Add the Sentry Android Gradle plugin in build.gradle:

Copied
buildscript {
  repositories {
    mavenCentral()
  }
}

plugins {
  id "io.sentry.android.gradle" version "3.1.0-beta.1"
}

Then, initialize the Android SDK.

The Android SDK automatically adds the FragmentLifecycleIntegration if the sentry-android-fragment dependency was found on the classpath. The integration is added with both enableFragmentLifecycleBreadcrumbs and enableAutoFragmentLifecycleTracing enabled.

However, you can still override the default behaviour by adding your own instance of the FragmentLifecycleIntegration. For that, refer to the manual installation section below.

Manual Installation

Install

To add the Fragment integration, manually initialize the Android SDK, then add the sentry-android-fragment dependency. Using Gradle:

Copied
implementation 'io.sentry:sentry-android:5.7.4'
implementation 'io.sentry:sentry-android-fragment:5.7.4'

Configure

Configuration should happen as early as possible in your application's lifecycle.

Copied
import android.app.Application
import io.sentry.android.core.SentryAndroid
import io.sentry.android.fragment.FragmentLifecycleIntegration

SentryAndroid.init(this) { options ->
    options.addIntegration(
        FragmentLifecycleIntegration(
            this,
            enableFragmentLifecycleBreadcrumbs = true, // enabled by default
            enableAutoFragmentLifecycleTracing = true  // disabled by default
            )
        )
}

Verify

Create a new Fragment view and add a button to it. This snippet captures an intentional message, so you can test that everything is working as soon as you set it up:

Copied
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import io.sentry.Sentry

class SampleFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        // generated databinding
        return FragmentSampleBinding.inflate(inflater).apply {
            this.sendMessage.setOnClickListener {
                Sentry.captureMessage("Some message from Fragment Lifecycle events in breadcrumbs.")
            }
        }.root
    }

    companion object {
        @JvmStatic fun newInstance() = SampleFragment()
    }
}
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) to suggesting an update ("yeah, this would be better").