أحداث لمرة واحدة باستخدام Channels
سلّم أحداث التنقل والتنبيهات بأمان
أحداث لمرة واحدة باستخدام Channels درس مجاني في Jetpack Compose Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Jetpack Compose Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Jetpack Compose Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
State Is Not an Event
Some things should happen exactly once, like navigating or showing a toast. Modeling these as state causes them to replay on every rotation.
The Replay Problem
If you store a snackbar message in StateFlow, a configuration change re-reads it and the message pops up again unexpectedly.
Meet the Channel
A Channel is a hot pipe where each value is delivered to exactly one receiver and then consumed, never replayed.
private val _events = Channel<UiEvent>()Send Versus Emit
You push into a Channel with send, a suspend call. The event waits in the buffer until something is ready to receive it.
_events.send(UiEvent.NavigateBack)Expose as a Flow
Turn the channel into a stream for the UI with receiveAsFlow, which lets exactly one collector consume each event.
val events = _events.receiveAsFlow()Consumed Once, Forever
Once a collector takes an event from the Channel, it is gone. A later rotation cannot read it again, so no accidental replay.
Model Events as a Type
Define a sealed UiEvent type so every one-off action, like navigation or messages, is explicit and exhaustive.
sealed interface UiEvent {
data class ShowToast(val msg: String) : UiEvent
}Collect Events in Compose
Collect the event flow inside a LaunchedEffect so it ties to the screen lifecycle and runs each event handler once.
LaunchedEffect(Unit) {
events.collect { handle(it) }
}Choose the Buffer Policy
A default Channel suspends the sender if no one receives. You can give it a buffer so sends never block your logic.
Channel<UiEvent>(Channel.BUFFERED)SharedFlow Is an Alternative
A SharedFlow with replay zero can also carry events, but a Channel guarantees single delivery even with no active collector.
Keep State and Events Apart
The clean pattern: one StateFlow for screen state and one Channel for fire-once events. They never get confused again.
Quick Check
Decide what fits a one-off action.
Recap: One-Off Events
Awesome! A Channel delivers fire-once events like navigation and toasts exactly once, keeping them separate from replayable screen state. 🎯
الأسئلة الشائعة
هل درس «أحداث لمرة واحدة باستخدام Channels» مجاني؟
نعم — نص درس «أحداث لمرة واحدة باستخدام Channels» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Jetpack Compose Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Jetpack Compose Academy 4 دروس في المجموع.
ماذا ستتعلم في «أحداث لمرة واحدة باستخدام Channels»؟
سلّم أحداث التنقل والتنبيهات بأمان تتمرن على Jetpack Compose Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Jetpack Compose Academy؟
لا تُشترط خبرة سابقة. Jetpack Compose Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «أحداث لمرة واحدة باستخدام Channels»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Jetpack Compose Academy هذا؟
نعم. كل درس في Jetpack Compose Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- StateFlow مقابل SharedFlow
- تعيين التدفقات ودمجها
- stateIn وWhileSubscribed
- أحداث لمرة واحدة باستخدام Channels