Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Các tính năng API để cải thiện khả năng xử lý lỗi trên Glance sẽ được đưa vào từ Android 15. Trang này cung cấp các phương pháp hay nhất liên quan đến những API này.
Sử dụng khối try-catch xung quanh các thành phần không kết hợp
Compose không cho phép các khối try-catch xung quanh thành phần kết hợp, nhưng cho phép bạn bao bọc logic khác của ứng dụng trong các khối này. Điều này cho phép bạn sử dụng Compose cho khung hiển thị lỗi, như trong ví dụ sau:
Nếu có một ngoại lệ chưa được phát hiện hoặc lỗi Compose, Glance sẽ hiển thị bố cục lỗi mặc định:
Hình 1. Bố cục lỗi mặc định của Glance 1.0Hình 2. Bố cục lỗi mặc định của Glance 1.1.0
Glance cho phép nhà phát triển cung cấp một bố cục XML làm phương án dự phòng nếu quá trình kết hợp không thành công. Điều này có nghĩa là đã xảy ra lỗi trong mã Compose. Giao diện người dùng lỗi này cũng xuất hiện nếu bạn gặp lỗi chưa được phát hiện trong mã của ứng dụng.
Bố cục này là một bố cục tĩnh mà người dùng không thể tương tác, nhưng rất hữu ích trong trường hợp khẩn cấp.
Hình 3. Ví dụ về bố cục lỗi tuỳ chỉnh
Thêm các thao tác vào giao diện người dùng lỗi mặc định
Kể từ Glance 1.1.0, Glance cho phép bạn ghi đè mã xử lý lỗi mặc định.
Bằng cách này, bạn có thể thêm các lệnh gọi lại hành động trong trường hợp có một ngoại lệ hoặc lỗi chưa được phát hiện trong thành phần.
Để dùng tính năng này, hãy ghi đè hàm onCompositionError():
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)}
Tạo một ý định đang chờ xử lý tham chiếu đến GlanceAppWidgetReceiver của bạn:
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-08-27 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-08-27 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 }"]]