0Pricing
Jetpack Compose Academy · บทเรียน

ขอบเขตและอายุการใช้งานขององค์ประกอบ

ควบคุมว่าการพึ่งพาจะมีชีวิตอยู่นานเท่าใด

ขอบเขตและอายุการใช้งานขององค์ประกอบ เป็นบทเรียน Jetpack Compose Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Jetpack Compose Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Jetpack Compose Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

What a Scope Controls

A scope decides how long Hilt keeps a single instance alive and where it gets shared. It is about lifetime, not just creation.

Unscoped by Default

Without a scope annotation, Hilt builds a new instance every time it is requested. That is fine for cheap, stateless objects.

@Provides
fun provideMapper(): Mapper = Mapper() // fresh each time

Components Mirror Android

Hilt provides a set of components that match Android lifetimes, from the whole app down to a single view. Each owns its own instances.

App-Wide With Singleton

SingletonComponent lives as long as the app. Bindings here with @Singleton are created once and shared across every screen.

@InstallIn(SingletonComponent::class)

Per-Activity Lifetime

ActivityComponent follows one Activity. Use @ActivityScoped to share an instance across that Activity and recreate it on a new one.

@ActivityScoped
class FormState @Inject constructor()

ViewModel Scope

ViewModelComponent matches a ViewModel's life. With @ViewModelScoped a dependency is shared only within that one ViewModel.

@ViewModelScoped
class SessionCache @Inject constructor()

Scope Equals Annotation Plus Component

Each scope annotation pairs with one component. @Singleton goes with SingletonComponent, @ActivityScoped with ActivityComponent, and so on.

Lifetimes Form a Hierarchy

Components nest: an Activity lives inside the app, a Fragment inside the Activity. A child can use anything its parents provide.

Scope With Care

Scoping costs memory, because the instance is held until its component dies. Only scope objects whose state genuinely needs to be shared.

Avoid Leaking the Wider Scope

Never inject a short-lived object, like an Activity, into a Singleton. The longer-lived holder would keep it alive and leak memory. ⚠️

Picking the Right Scope

Match the scope to the data's life: app config in Singleton, screen state in ViewModelScoped. The right fit prevents leaks and waste.

Quick Check

You want one shared instance for the entire app lifetime. Which scope fits?

Recap: Lifetimes Under Control

You learned that scopes tie an instance's lifetime to a component, from Singleton down to ViewModel, so dependencies live exactly as long as needed. 🎯

คำถามที่พบบ่อย

บทเรียน “ขอบเขตและอายุการใช้งานขององค์ประกอบ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ขอบเขตและอายุการใช้งานขององค์ประกอบ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Jetpack Compose Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Jetpack Compose Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ขอบเขตและอายุการใช้งานขององค์ประกอบ”

ควบคุมว่าการพึ่งพาจะมีชีวิตอยู่นานเท่าใด คุณปฏิบัติ Jetpack Compose Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Jetpack Compose Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Jetpack Compose Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “ขอบเขตและอายุการใช้งานขององค์ประกอบ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Jetpack Compose Academy นี้ได้ไหม

ได้ บทเรียน Jetpack Compose Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. เหตุใดจึงต้องใช้ DI: โค้ดที่ทดสอบได้และแยกส่วน
  2. โมดูล @Provides และ @Binds
  3. hiltViewModel และ @HiltViewModel
  4. ขอบเขตและอายุการใช้งานขององค์ประกอบ
← กลับไปที่ Jetpack Compose Academy