Fragment

Learn more about the Sentry Fragment integration for the Android SDK.

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 from an active transaction that's bound to the scope of each launch of a fragment.

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
plugins {
  id "io.sentry.android.gradle" version "4.10.0"
}

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.

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:7.12.1'
implementation 'io.sentry:sentry-android-fragment:7.12.1'

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
            )
        )
}

You can also configure which fragment lifecycle events will be added as breadcrumbs.

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

SentryAndroid.init(this) { options ->
    options.addIntegration(
        FragmentLifecycleIntegration(
            this,
            filterFragmentLifecycleBreadcrumbs = setOf(FragmentLifecycleState.CREATED, FragmentLifecycleState.DESTROYED)
            )
        )
}

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!) or suggesting an update ("yeah, this would be better").