Stay organized with collections
Save and categorize content based on your preferences.
API features for improving error handling on Glance are included beginning in
Android 15. This page provides best practices regarding these APIs.
Use a try-catch block around non-composable components
Compose doesn't allow try-catch blocks around composables, but lets you wrap
your app's other logic in these blocks. This lets you use Compose for your
error view, as shown in the following example:
Glance lets developers provide an XML layout as a fallback if composition
fails. This means that there was an error in the Compose code. This error UI
also appears if you have an uncaught error in your app's code.
This layout is a static layout that your user can't interact with, but is good
in emergency cases.
Figure 3. Example custom error layout
Add actions to the default error UI
As of Glance 1.1.0, Glance lets you override the default error handling code.
This way, you can add action callbacks in the event of an uncaught exception or
error in composition.
To use this feature, override the onCompositionError() function:
overridefunonCompositionError(context:Context,glanceId:GlanceId,appWidgetId:Int,throwable:Throwable){valrv=RemoteViews(context.packageName,R.layout.error_layout)rv.setTextViewText(R.id.error_text_view,"Error was thrown. \nThis is a custom view \nError Message: `${throwable.message}`")rv.setOnClickPendingIntent(R.id.error_icon,getErrorIntent(context,throwable))AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId,rv)}
Create a pending intent that references your GlanceAppWidgetReceiver:
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-08-26 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-26 UTC."],[],[],null,["API features for improving error handling on Glance are included beginning in\nAndroid 15. This page provides best practices regarding these APIs.\n\nUse a try-catch block around non-composable components\n\nCompose doesn't allow try-catch blocks around composables, but lets you wrap\nyour app's other logic in these blocks. This lets you use Compose for your\nerror view, as shown in the following example: \n\n provideContent {\n var isError = false;\n var data = null\n try {\n val repository = (context.applicationContext as MyApplication).myRepository\n data = repository.loadData()\n } catch (e: Exception) {\n isError = true;\n //handleError\n }\n\n if (isError) {\n ErrorView()\n } else {\n Content(data)\n }\n }\n\nDefault error layout\n\nIf there is an uncaught exception or a Compose error, Glance displays a\ndefault error layout:\n**Figure 1.**Glance 1.0 default error layout **Figure 2.**Glance 1.1.0 default error layout\n\nGlance lets developers provide an XML layout as a fallback if composition\nfails. This means that there was an error in the Compose code. This error UI\nalso appears if you have an uncaught error in your app's code. \n\n class UpgradeWidget : GlanceAppWidget(errorUiLayout = R.layout.error_layout)\n\nThis layout is a static layout that your user can't interact with, but is good\nin emergency cases.\n**Figure 3.**Example custom error layout\n\nAdd actions to the default error UI\n\nAs of Glance 1.1.0, Glance lets you override the default error handling code.\nThis way, you can add action callbacks in the event of an uncaught exception or\nerror in composition.\n\nTo use this feature, override the `onCompositionError()` function: \n\n GlanceAppWidget.onCompositionError(\n context: Context,\n glanceId: GlanceId,\n appWidgetId: Int,\n throwable: Throwable\n )\n\nIn this function, Glance falls back to the `RemoteViews` API for error handling.\nThis lets you specify layouts and action handlers using XML.\n\nThe following examples show you, step-by-step, how to create an error UI that\nincludes a button to send feedback:\n\n1. Write the `error_layout.xml` file:\n\n \u003cLinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n style=\"@style/Widget.MyApplication.AppWidget.Error\"\n android:id=\"@android:id/background\"\n android:layout_width=\"match_parent\"\n android:textSize=\"24sp\"\n android:layout_height=\"match_parent\"\n android:orientation=\"vertical\"\u003e\n\n \u003cTextView\n android:id=\"@+id/error_title_view\"\n android:layout_width=\"match_parent\"\n android:textColor=\"@color/white\"\n android:textFontWeight=\"800\"\n android:layout_height=\"wrap_content\"\n android:text=\"Example Widget Error\" /\u003e\n\n \u003cLinearLayout\n android:layout_width=\"match_parent\"\n android:orientation=\"horizontal\"\n android:paddingTop=\"4dp\"\n android:layout_height=\"match_parent\"\u003e\n\n \u003cImageButton\n android:layout_width=\"64dp\"\n android:layout_height=\"64dp\"\n android:layout_gravity=\"center\"\n android:tint=\"@color/white\"\n android:id=\"@+id/error_icon\"\n android:src=\"@drawable/heart_broken_fill0_wght400_grad0_opsz24\"\n /\u003e\n \u003cTextView\n android:id=\"@+id/error_text_view\"\n android:layout_width=\"wrap_content\"\n android:textColor=\"@color/white\"\n android:layout_height=\"wrap_content\"\n android:layout_gravity=\"center\"\n android:padding=\"8dp\"\n android:textSize=\"16sp\"\n android:layout_weight=\"1\"\n android:text=\"Useful Error Message!\" /\u003e\n \u003c/LinearLayout\u003e\n\n \u003c/LinearLayout\u003e\n\n2. Override the `onCompositionError` function:\n\n override fun onCompositionError(\n context: Context,\n glanceId: GlanceId,\n appWidgetId: Int,\n throwable: Throwable\n ) {\n val rv = RemoteViews(context.packageName, R.layout.error_layout)\n rv.setTextViewText(\n R.id.error_text_view,\n \"Error was thrown. \\nThis is a custom view \\nError Message: `${throwable.message}`\"\n )\n rv.setOnClickPendingIntent(R.id.error_icon, getErrorIntent(context, throwable))\n AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, rv)\n }\n\n3. Create a pending intent that references your `GlanceAppWidgetReceiver`:\n\n private fun getErrorIntent(context: Context, throwable: Throwable): PendingIntent {\n val intent = Intent(context, UpgradeToHelloWorldPro::class.java)\n intent.setAction(\"widgetError\")\n return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)\n }\n\n4. Handle the intent in your `GlanceAppWidgetReceiver`:\n\n override fun onReceive(context: Context, intent: Intent) {\n super.onReceive(context, intent)\n Log.e(\"ErrorOnClick\", \"Button was clicked.\");\n }"]]