Glance でエラーを処理する

Glance でのエラー処理を改善するための API 機能は、Android 15 以降に含まれています。このページでは、これらの API に関するベスト プラクティスについて説明します。

コンポーズ可能なコンポーネントを try-catch ブロックで囲む

Compose では、コンポーザブルの周囲に try-catch ブロックを使用することはできませんが、アプリの他のロジックをこれらのブロックでラップできます。これにより、次の例に示すように、Compose をエラービューに使用できます。

provideContent {
       var isError = false;
       var data = null
       try {
           val repository = (context.applicationContext as MyApplication).myRepository
           data = repository.loadData()
       } catch (e: Exception) {
           isError = true;
           //handleError
       }

       if (isError) {
           ErrorView()
       } else {
           Content(data)
       }
   }

デフォルトのエラー レイアウト

キャッチされていない例外や Compose エラーが発生した場合、Glance にはデフォルトのエラー レイアウトが表示されます。

エラーの種類とエラーの検索場所を示すエラー メッセージ
図 1. Glance 1.0 のデフォルトのエラー レイアウト
「コンテンツを表示できません」と表示されたボックス
図 2. Glance 1.1.0 のデフォルトのエラー レイアウト

Glance を使用すると、デベロッパーはコンポジションが失敗した場合のフォールバックとして XML レイアウトを指定できます。これは、Compose コードでエラーが発生したことを意味します。このエラー UI は、アプリのコード内に捕捉されていないエラーがある場合にも表示されます。

class UpgradeWidget : GlanceAppWidget(errorUiLayout = R.layout.error_layout)

このレイアウトはユーザーが操作できない静的なレイアウトですが、緊急時に適しています。

エラー メッセージを表示する見出しとテキスト フィールドが含まれています
図 3. カスタム エラー レイアウトの例

デフォルトのエラー UI にアクションを追加する

Glance 1.1.0 以降では、Glance でデフォルトのエラー処理コードをオーバーライドできます。これにより、コンポジションでキャッチされない例外やエラーが発生した場合にアクション コールバックを追加できます。

この機能を使用するには、onCompositionError() 関数をオーバーライドします。

GlanceAppWidget.onCompositionError(
    context: Context,
    glanceId: GlanceId,
    AppWidgetId: Int,
    throwable: Throwable
)

この関数では、Glance はエラー処理のために RemoteViews API にフォールバックします。これにより、XML を使用してレイアウトとアクション ハンドラを指定できます。

次の例は、フィードバックを送信するボタンを含むエラー UI を作成する方法を示しています。

  1. error_layout.xml ファイルを作成する
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   style="@style/Widget.MyApplication.AppWidget.Error"
   android:id="@android:id/background"
   android:layout_width="match_parent"
   android:textSize="24sp"
   android:layout_height="match_parent"
   android:orientation="vertical">


   <TextView
       android:id="@+id/error_title_view"
       android:layout_width="match_parent"
       android:textColor="@color/white"
       android:textFontWeight="800"
       android:layout_height="wrap_content"
       android:text="Example Widget Error" />

   <LinearLayout
       android:layout_width="match_parent"
       android:orientation="horizontal"
       android:paddingTop="4dp"
       android:layout_height="match_parent">

       <ImageButton
           android:layout_width="64dp"
           android:layout_height="64dp"
           android:layout_gravity="center"
           android:tint="@color/white"
           android:id="@+id/error_icon"
           android:src="@drawable/heart_broken_fill0_wght400_grad0_opsz24"
       />
       <TextView
           android:id="@+id/error_text_view"
           android:layout_width="wrap_content"
           android:textColor="@color/white"
           android:layout_height="wrap_content"
           android:layout_gravity="center"
           android:padding="8dp"
           android:textSize="16sp"
           android:layout_weight="1"
           android:text="Useful Error Message!" />
   </LinearLayout>

</LinearLayout>

  1. onCompositionError 関数をオーバーライドする
override fun onCompositionError(
   context: Context,
   glanceId: GlanceId,
   AppWidgetId: Int,
   throwable: Throwable
) {
   val rv = 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)
}

  1. GlanceAppWidgetReceiver を参照するペンディング インテントを作成する
private fun getErrorIntent(context: Context, throwable: Throwable): PendingIntent {
    val intent = Intent(context, UpgradeToHelloWorldPro::class.java)
    intent.setAction("widgetError")
    return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)
}

  1. GlanceAppWidgetReceiver でインテントを処理する
override fun onReceive(context: Context, intent: Intent) {
   super.onReceive(context, intent)
   Log.e("ErrorOnClick", "Button was clicked.");
}