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.

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

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