چرخه زندگی

مولفه های آگاه از چرخه حیات، اقداماتی را در پاسخ به تغییر وضعیت چرخه حیات یک جزء دیگر، مانند فعالیت ها و قطعات، انجام می دهند. این مولفه ها به شما کمک می کنند کدی با سازماندهی بهتر و اغلب سبک تر تولید کنید که نگهداری آن آسان تر است.

این جدول تمام مصنوعات موجود در گروه androidx.lifecycle را فهرست می کند.

مصنوع انتشار پایدار کاندید را آزاد کنید نسخه بتا انتشار آلفا
چرخه زندگی-* 2.9.4 - - 2.10.0-alpha05
lifecycle-viewmodel-navigation3 - - - 2.10.0-alpha05
آخرین به روز رسانی این کتابخانه در: 8 اکتبر 2025

اعلام وابستگی ها

برای افزودن وابستگی به Lifecycle، باید مخزن Google Maven را به پروژه خود اضافه کنید. برای اطلاعات بیشتر، مخزن Maven Google را بخوانید.

وابستگی‌های مصنوعات مورد نیاز خود را در فایل build.gradle برای برنامه یا ماژول خود اضافه کنید:

کاتلین

شیار

    dependencies {
        def lifecycle_version = "2.9.4"
        def arch_version = "2.2.0"

        // ViewModel
        implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
        // ViewModel utilities for Compose
        implementation "androidx.lifecycle:lifecycle-viewmodel-compose:$lifecycle_version"
        // LiveData
        implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
        // Lifecycles only (without ViewModel or LiveData)
        implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
        // Lifecycle utilities for Compose
        implementation "androidx.lifecycle:lifecycle-runtime-compose:$lifecycle_version"

        // Saved state module for ViewModel
        implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version"

        // ViewModel integration with Navigation3
        implementation "androidx.lifecycle:lifecycle-viewmodel-navigation3:2.10.0-alpha05"

        // Annotation processor
        kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
        // alternately - if using Java8, use the following instead of lifecycle-compiler
        implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"

        // optional - helpers for implementing LifecycleOwner in a Service
        implementation "androidx.lifecycle:lifecycle-service:$lifecycle_version"

        // optional - ProcessLifecycleOwner provides a lifecycle for the whole application process
        implementation "androidx.lifecycle:lifecycle-process:$lifecycle_version"

        // optional - ReactiveStreams support for LiveData
        implementation "androidx.lifecycle:lifecycle-reactivestreams-ktx:$lifecycle_version"

        // optional - Test helpers for LiveData
        testImplementation "androidx.arch.core:core-testing:$arch_version"

        // optional - Test helpers for Lifecycle runtime
        testImplementation "androidx.lifecycle:lifecycle-runtime-testing:$lifecycle_version"
    }
    

کاتلین

    dependencies {
        val lifecycle_version = "2.9.4"
        val arch_version = "2.2.0"

        // ViewModel
        implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version")
        // ViewModel utilities for Compose
        implementation("androidx.lifecycle:lifecycle-viewmodel-compose:$lifecycle_version")
        // LiveData
        implementation("androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version")
        // Lifecycles only (without ViewModel or LiveData)
        implementation("androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version")
        // Lifecycle utilities for Compose
        implementation("androidx.lifecycle:lifecycle-runtime-compose:$lifecycle_version")

        // Saved state module for ViewModel
        implementation("androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version")

        // ViewModel integration with Navigation3
        implementation("androidx.lifecycle:lifecycle-viewmodel-navigation3:2.10.0-alpha05")

        // Annotation processor
        kapt("androidx.lifecycle:lifecycle-compiler:$lifecycle_version")
        // alternately - if using Java8, use the following instead of lifecycle-compiler
        implementation("androidx.lifecycle:lifecycle-common-java8:$lifecycle_version")

        // optional - helpers for implementing LifecycleOwner in a Service
        implementation("androidx.lifecycle:lifecycle-service:$lifecycle_version")

        // optional - ProcessLifecycleOwner provides a lifecycle for the whole application process
        implementation("androidx.lifecycle:lifecycle-process:$lifecycle_version")

        // optional - ReactiveStreams support for LiveData
        implementation("androidx.lifecycle:lifecycle-reactivestreams-ktx:$lifecycle_version")

        // optional - Test helpers for LiveData
        testImplementation("androidx.arch.core:core-testing:$arch_version")

        // optional - Test helpers for Lifecycle runtime
        testImplementation ("androidx.lifecycle:lifecycle-runtime-testing:$lifecycle_version")
    }
    

جاوا

شیار

    dependencies {
        def lifecycle_version = "2.9.4"
        def arch_version = "2.2.0"

        // ViewModel
        implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
        // LiveData
        implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
        // Lifecycles only (without ViewModel or LiveData)
        implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"

        // Saved state module for ViewModel
        implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version"

        // Annotation processor
        annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
        // alternately - if using Java8, use the following instead of lifecycle-compiler
        implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"

        // optional - helpers for implementing LifecycleOwner in a Service
        implementation "androidx.lifecycle:lifecycle-service:$lifecycle_version"

        // optional - ProcessLifecycleOwner provides a lifecycle for the whole application process
        implementation "androidx.lifecycle:lifecycle-process:$lifecycle_version"

        // optional - ReactiveStreams support for LiveData
        implementation "androidx.lifecycle:lifecycle-reactivestreams:$lifecycle_version"

        // optional - Test helpers for LiveData
        testImplementation "androidx.arch.core:core-testing:$arch_version"

        // optional - Test helpers for Lifecycle runtime
        testImplementation "androidx.lifecycle:lifecycle-runtime-testing:$lifecycle_version"
    }
    

کاتلین

    dependencies {
        val lifecycle_version = "2.9.4"
        val arch_version = "2.2.0"

        // ViewModel
        implementation("androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version")
        // LiveData
        implementation("androidx.lifecycle:lifecycle-livedata:$lifecycle_version")
        // Lifecycles only (without ViewModel or LiveData)
        implementation("androidx.lifecycle:lifecycle-runtime:$lifecycle_version")

        // Saved state module for ViewModel
        implementation("androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version")

        // Annotation processor
        annotationProcessor("androidx.lifecycle:lifecycle-compiler:$lifecycle_version")
        // alternately - if using Java8, use the following instead of lifecycle-compiler
        implementation("androidx.lifecycle:lifecycle-common-java8:$lifecycle_version")

        // optional - helpers for implementing LifecycleOwner in a Service
        implementation("androidx.lifecycle:lifecycle-service:$lifecycle_version")

        // optional - ProcessLifecycleOwner provides a lifecycle for the whole application process
        implementation("androidx.lifecycle:lifecycle-process:$lifecycle_version")

        // optional - ReactiveStreams support for LiveData
        implementation("androidx.lifecycle:lifecycle-reactivestreams:$lifecycle_version")

        // optional - Test helpers for LiveData
        testImplementation("androidx.arch.core:core-testing:$arch_version")

        // optional - Test helpers for Lifecycle runtime
        testImplementation("androidx.lifecycle:lifecycle-runtime-testing:$lifecycle_version")
    }
    

برای اطلاعات بیشتر درباره وابستگی‌ها، به افزودن وابستگی‌های ساخت مراجعه کنید.

بازخورد

بازخورد شما به بهتر شدن Jetpack کمک می کند. اگر مسائل جدیدی کشف کردید یا ایده هایی برای بهبود این کتابخانه دارید، به ما اطلاع دهید. لطفاً قبل از ایجاد کتابخانه جدید، به مسائل موجود در این کتابخانه نگاهی بیندازید. با کلیک کردن روی دکمه ستاره می توانید رای خود را به یک موضوع موجود اضافه کنید.

یک شماره جدید ایجاد کنید

برای اطلاعات بیشتر به مستندات ردیاب مشکل مراجعه کنید.

نسخه 2.10

نسخه 2.10.0-alpha05

08 اکتبر 2025

androidx.lifecycle:lifecycle-*:2.10.0-alpha05 منتشر شد. نسخه 2.10.0-alpha05 حاوی این commit ها است.

تغییرات API

  • removeViewModelStoreOnPopCallback() اکنون بخشی از یک شی ViewModelStoreNavEntryDecoratorDefault است که در آن پلتفرم ها و پیاده سازی های دیگر می توانند پیش فرض را فراخوانی کنند. ( Ia1f23 , b/444447434 )
  • ViewModelStoreNavEntryDecorator از یک تابع به یک کلاس تغییر داد تا عملکرد آن را به عنوان یک کارخانه برای NavEntryDecorator بهتر منعکس کند، و نام پارامتر shouldRemoveViewModelStore دکوراتور را به removeViewModelStoreOnPop تغییر داد تا مشخص شود که این بازخوانی تنها زمانی از ورودی backStack می شود. ( Iefdc5 , b/444447434 )

نسخه 2.10.0-alpha04

24 سپتامبر 2025

androidx.lifecycle:lifecycle-*:2.10.0-alpha04 منتشر شد. نسخه 2.10.0-alpha04 حاوی این commit ها است.

تغییرات API

  • Refactor LifecycleOwner قابل تنظیم برای rememberLifecycleOwner . اکنون تابع به طور مستقیم LifecycleOwner را برمی گرداند. برای ارائه این مالک به یک ترکیب فرعی، از CompositionLocalProvider استفاده کنید. ( IC57f0 , b/444446629 )
  • خردهای KMP را به lifecycle-viewmodel-navigation3 اضافه کنید تا JetBrains را قادر سازد تا فورک هایی را فراهم کند که این اهداف را پر می کند و بنابراین از CMP پشتیبانی می کند. ( I44a4c )

نسخه 2.10.0-alpha03

27 آگوست 2025

androidx.lifecycle:lifecycle-*:2.10.0-alpha03 منتشر شد. نسخه 2.10.0-alpha03 حاوی این commit ها است.

تغییرات API

  • Compose را به 1.9.0 به روز کنید. ( I2b9de )

نسخه 2.10.0-alpha02

13 آگوست 2025

androidx.lifecycle:lifecycle-*:2.10.0-alpha02 منتشر شد. نسخه 2.10.0-alpha02 حاوی این commit ها است.

ویژگی های جدید

  • Composable LifecycleOwner اکنون می تواند یک چرخه حیات ریشه مستقل ایجاد کند. با (صراحتا) تنظیم parent = null ، چرخه حیات جدید مستقل از هر میزبانی (مانند یک Activity ، Fragment یا NavBackStackEntry ) عمل می کند. به محض ورود ترکیب‌پذیر به ترکیب شروع می‌شود و با خروج به‌طور خودکار از بین می‌رود. ( I8dfbe , b/433659048 )

    @Composable
    fun IndependentComponent() {
        // Create a standalone lifecycle, not tied to the parent Activity/Fragment.
        LifecycleOwner(parent = null) {
            val rootLifecycle = LocalLifecycleOwner.current.lifecycle
        }
    }
    

تغییرات API

  • در Composable LifecycleOwner ، پارامتر parentLifecycleOwner به parent تغییر نام داده شده است. ( I080bc )

رفع اشکال

  • LifecycleOwner composable اکنون چرخه عمر خود را به درستی به DESTROYED پس از دفع می‌برد. این امر از نشت احتمالی کدهای خارجی که دارای ارجاع به چرخه حیات هستند جلوگیری می کند. ( I9e5b7 , b/433659048 )
  • انتقال minSdk پیش‌فرض از API 21 به API 23 ( Ibdfca , b/380448311 , b/435705964 , b/435705223 )

مشارکت خارجی

  • فیلدهای پشتیبان داخلی غیرضروری را از LifecycleOwner حذف کنید. با تشکر از جیک وارتون برای مشارکت. ( Ideddb )

نسخه 2.10.0-alpha01

30 جولای 2025

androidx.lifecycle:lifecycle-*:2.10.0-alpha01 منتشر شد. نسخه 2.10.0-alpha01 حاوی این commit ها است.

ویژگی های جدید

  • یک LifecycleOwner قابل ترکیب اضافه کنید تا ایجاد چرخه‌های حیات محدوده‌دار را مستقیماً در UI فعال کنید. این برای اجزایی که نیاز به مدیریت مستقل چرخه عمر خود دارند مفید است. برای مثالی از نحوه ادغام Navigation3 این composable جدید، aosp/3708610 را ببینید. ( 76cbf7 )

    @Composable
    fun MyComposable() {
        LifecycleOwner(
            maxLifecycle = RESUMED,
            parentLifecycleOwner = LocalLifecycleOwner.current,
        ) {
            val childLifecycleOwner = LocalLifecycleOwner.current
        }
    }
    

تغییرات API

  • یک تابع کارخانه سازنده برای CreationExtras اضافه کنید، یک API راحت تر و اصطلاحی Kotlin ارائه می دهد. ( Iab2bd )
  • پشتیبانی بومی برای انواع nullable در SavedStateHandle.saved اضافه کنید، که ذخیره و بازیابی ویژگی های nullable را ساده می کند. ( I54d69 , b/421325690 )
  • سازنده‌های SavedStateHandle به‌عنوان @VisibleForTesting علامت‌گذاری کنید. ( Iff0e0 , b/408002794 )

نسخه 2.9

نسخه 2.9.4

17 سپتامبر 2025

androidx.lifecycle:lifecycle-*:2.9.4 منتشر شد. نسخه 2.9.4 حاوی این commit ها است.

رفع اشکال

  • خطای عدم استفاده از افزونه Compose Compiler که باعث شکسته شدن مصنوعات Lifecycle KMP شده بود، برطرف شد. ( Ie95bc , b/443096483 , b/443965665 )

نسخه 2.9.3

27 آگوست 2025

androidx.lifecycle:lifecycle-*:2.9.3 منتشر شد. نسخه 2.9.3 شامل این commit ها است.

ویژگی های جدید

  • اهداف جدید Kotlin Multiplatform (KMP) را به آرتیفکت های Lifecycle *-compose اضافه کنید. Lifecycle اکنون در مجموع از پلتفرم‌های زیر پشتیبانی می‌کند: JVM (اندروید و دسکتاپ)، Native (Linux، iOS، watchOS، macOS، MinGW) و وب (جاوا اسکریپت، WasmJS). ( I0a0e4 )

رفع اشکال

نسخه 2.9.2

16 جولای 2025

androidx.lifecycle:lifecycle-*:2.9.2 منتشر شد. نسخه 2.9.2 حاوی این commit ها است.

رفع اشکال

  • اهداف جدید Kotlin Multiplatform (KMP) به مصنوعات چرخه حیات اضافه شد. Lifecycle اکنون در مجموع از پلتفرم‌های زیر پشتیبانی می‌کند: JVM (اندروید و دسکتاپ)، Native (Linux، iOS، watchOS، macOS، MinGW) و وب (جاوا اسکریپت، WasmJS). توجه داشته باشید که هیچ هدف KMP جدیدی به آرتیفکت های *-compose اضافه نشده است، زیرا این امر به انتشار پایدار Compose 1.9 بستگی دارد. ( I01cb8 ).

به روز رسانی وابستگی

نسخه 2.9.1

4 ژوئن 2025

androidx.lifecycle:lifecycle-*:2.9.1 منتشر شد. نسخه 2.9.1 شامل این commit ها است.

رفع اشکال

  • SavedStateHandle.remove(key) حالت های SavedStateHandle.getMutableStateFlow(key) را پاک نمی کند. ( d5f939 , b/418746333 )

نسخه 2.9.0

7 مه 2025

androidx.lifecycle:lifecycle-*:2.9.0 منتشر شد. نسخه 2.9.0 شامل این commit ها است.

تغییرات مهم از 2.8.0

  • یک مصنوع KMP جدید androidx.lifecycle:lifecycle-viewmodel-testing موجود است که یک کلاس ViewModelScenario را برای آزمایش ViewModels به صورت مجزا، با پشتیبانی از onCleared و SavedStateHandle و همچنین آزمایش مرگ و بازآفرینی فرآیند از طریق recreate() ارائه می‌کند.
  • برای بازگشت یک MutableStateFlow getMutableStateFlow به SavedStateHandle اضافه کنید. این تابع جدید انحصاری کلید است و با getLiveData قابل استفاده نیست. اگر سعی کنید از هر دو برای دسترسی به یک حالت استفاده کنید، یک استثنا ایجاد می شود.
  • CreationExtras اکنون شامل اضافه بارهای اپراتور شبیه به نقشه برای فعال کردن دستکاری اصطلاحی محتوا در Kotlin است. این اجازه می دهد تا از in ، += ، و + با CreationExtras استفاده کنید.

پشتیبانی از سریال سازی KotlinX

  • با پشتیبانی از KotlinX Serialization که در SavedState 1.3.0 اضافه شده است، saved معرفی کرده‌ایم، یک نماینده ویژگی تنبل، تا ذخیره کلاس‌های @Serializable در SavedStateHandle را آسان کنیم و آن کلاس‌ها به‌طور خودکار در طول فرآیند مرگ و بازیابی بازیابی شوند. لطفاً توجه داشته باشید که نماینده saved تنبل است و تا زمانی که به آن دسترسی پیدا نشود، init lambda را صدا نمی کند یا چیزی را در SavedStateHandle ذخیره نمی کند.

    @Serializable
    data class Person(val firstName: String, val lastName: String)
    
    class MyViewModel(handle: SavedStateHandle) : ViewModel() {
        var person by handle.saved { Person("John", "Doe") }
    
        fun onPersonChanged(person: Person) {
            this.person = person
        }
    }
    

چند پلتفرم کاتلین

  • ماژول lifecycle-testing اکنون با KMP سازگار است از جمله APIهایی مانند TestLifecycleOwner .
  • ماژول lifecycle-viewmodel-savedstate اکنون با KMP از جمله APIهایی مانند SavedStateHandle سازگار است.
  • androidx.compose.ui.platform.LocalLifecycleOwner اکنون در مجموعه منبع مشترک موجود است.
  • NewInstanceFactory اکنون در JVM Desktop و اهداف Android در دسترس است.

تغییرات رفتار

  • حالت Lifecycle.DESTROYED پایانه است و هر تلاشی برای انتقال یک Lifecycle از آن به هر حالت دیگری اکنون منجر به یک IllegalStateException می شود.
  • SavedStateHandle دیگر شامل SavedStateProvider.saveState() نمی شود که در آن Bundle بازگشتی خالی باشد.

نسخه 2.9.0-rc01

23 آوریل 2025

androidx.lifecycle:lifecycle-*:2.9.0-rc01 منتشر شد. نسخه 2.9.0-rc01 حاوی این commit ها است.

هشدار سازگاری Lint API

  • JetBrains KaCallableMemberCall از یک کلاس به یک رابط تغییر داد که سازگاری باینری را از بین می برد. اگر نسخه AGP پروژه شما با نسخه ای که برای کامپایل بررسی های لینت استفاده می شود متفاوت باشد، این می تواند باعث خرابی شود. این به‌روزرسانی در aosp/3577172 ساخته شده است، اما در یادداشت‌های انتشار وجود ندارد - ما در اینجا آن را توضیح می‌دهیم. راه حل توصیه شده: به آخرین AGP پایدار به روز کنید. اگر نمی‌توانید به‌طور کامل به‌روزرسانی کنید، از android.experimental.lint.version برای تراز کردن بررسی‌های پرز با نسخه AGP خود استفاده کنید — برای جزئیات به نوشتن تغییرات رفتار زمان اجرا مراجعه کنید.

نسخه 2.9.0-beta01

9 آوریل 2025

androidx.lifecycle:lifecycle-*:2.9.0-beta01 منتشر شد. نسخه 2.9.0-beta01 حاوی این تعهدات است.

تغییرات API

  • Lifecycle ViewModel Compose اکنون از همان تنظیمات Multiplatform Kotlin مانند Compose Runtime نسخه 1.7.1 و بالاتر استفاده می کند - مصنوعات -desktop اکنون حذف شده اند و مصنوعات -jvmStubs و -linuxx64Stubs اضافه شده اند. هیچ یک از این اهداف قرار نیست مورد استفاده قرار گیرند، آنها مکان هایی هستند که به تلاش های Jetbrains Compose کمک می کنند. ( I5cb14 , b/406592090 )

به روز رسانی های وابستگی

  • این کتابخانه اکنون سطح زبان Kotlin 2.0 را هدف قرار می دهد و به KGP 2.0.0 یا جدیدتر نیاز دارد. ( Idb6b5 )
  • Lifecycle ViewModel Compose اکنون به Compose 1.7.8 بستگی دارد. ( I5cb14 , b/406592090 )

نسخه 2.9.0-alpha13

26 مارس 2025

androidx.lifecycle:lifecycle-*:2.9.0-alpha13 بدون هیچ تغییر عمومی قابل توجهی منتشر شد. نسخه 2.9.0-alpha13 حاوی این commit ها است.

نسخه 2.9.0-alpha12

12 مارس 2025

androidx.lifecycle:lifecycle-*:2.9.0-alpha12 منتشر شد. نسخه 2.9.0-alpha12 حاوی این commit ها است.

تغییرات API

  • حاشیه نویسی @MainThread را به ViewModelProvider.get در همه سیستم عامل های KMP پشتیبانی شده اضافه کنید. ( I7e8dd , b/397736115 )
  • تغییر نام SavedState*Delegates به SavedState*Delegate . ( I8589b , b/399629301 )

نسخه 2.9.0-alpha11

26 فوریه 2025

androidx.lifecycle:lifecycle-*:2.9.0-alpha11 منتشر شد. نسخه 2.9.0-alpha11 حاوی این commit ها است.

تغییرات API

  • افزودن پارامتر SavedStateConfig به نمایندگان saved() ( I39b3a )

نسخه 2.9.0-alpha10

12 فوریه 2025

androidx.lifecycle:lifecycle-*:2.9.0-alpha10 منتشر شد. نسخه 2.9.0-alpha10 حاوی این commit ها است.

تغییرات API

  • MutableStateSerializer به savedstate-compose از lifecycle-viewmodel-compose منتقل کنید. ( I4f690 , b/378895074 )

مشارکت خارجی

  • یک مشکل جدید Lint را برای فراخوانی Lifecycle::currentState در ترکیب اضافه می‌کند، به جای آن پیشنهاد می‌کند که از currentStateAsalue().value تا اطمینان حاصل شود که تغییرات در حالت چرخه زندگی به درستی باعث ترکیب مجدد می‌شود. متشکرم استیون شوئن! ( Iad484 )

نسخه 2.9.0-alpha09

29 ژانویه 2025

androidx.lifecycle:lifecycle-*:2.9.0-alpha09 منتشر شد. نسخه 2.9.0-alpha09 حاوی این commit ها است.

ویژگی های جدید

  • MutableStateSerializer برای سریال سازی androidx.compose.runtime.MutableState اضافه کنید. ( Idfc48 , b/378895074 )

تغییرات API

  • توابع ارسال شده SavedStateHandle.saved() با پارامترهای پیش فرض جایگزین کنید ( Icd1c1 )
  • AbstractSavedStateViewModelFactory منسوخ شده است زیرا یک SavedStateHandle برای هر ViewModel ایجاد می کند و باعث سربار غیر ضروری می شود. برای ایجاد کارآمدتر ViewModel به جای آن از ViewModelProvider.Factory با CreationExtras.createSavedStateHandle استفاده کنید. ( Ia920b , b/388590327 )

نسخه 2.9.0-alpha08

11 دسامبر 2024

androidx.lifecycle:lifecycle-*:2.9.0-alpha08 منتشر شد. نسخه 2.9.0-alpha08 حاوی این تعهدات است.

ویژگی های جدید

  • ViewModelScenario.recreate را اضافه کنید تا یک System Process Death شبیه سازی شود که ViewModel تحت آزمایش و تمام اجزای مرتبط را بازسازی می کند. ( ID6a69 , b/381063087 )
  • نمونه‌های LifecycleOwner و ViewModelStoreOwner که از طریق APIهای findViewTree مربوطه خود بازیابی می‌شوند، اکنون می‌توانند از طریق والدین مجزای یک view، مانند ViewOverlay ، حل شوند. برای اطلاعات بیشتر در مورد والدین با نمای جدا، به یادداشت‌های انتشار هسته یا مستندات در ViewTree.setViewTreeDisjointParent مراجعه کنید. ( I800f4 )

تغییرات API

  • نام‌گذاری و سازمان‌دهی بسته‌ها را با SavedStateRegistryOwnerDelegate سازگارتر کنید ( I8c135 , b/376026744 )

رفع اشکال

  • این کتابخانه اکنون از حاشیه‌نویسی‌های پوچ JSpecify استفاده می‌کند که نوع استفاده هستند. توسعه دهندگان Kotlin باید از آرگومان کامپایلر زیر برای اعمال استفاده صحیح استفاده کنند: -Xjspecify-annotations=strict (این پیش فرض با نسخه 2.1.0 کامپایلر Kotlin شروع می شود). ( Ie4340 , b/326456246 )
  • Document ViewModel.onCleared توالی پاکسازی پاک شده. ( I586c7 , b/363984116 )

نسخه 2.9.0-alpha07

13 نوامبر 2024

androidx.lifecycle:lifecycle-*:2.9.0-alpha07 منتشر شد. نسخه 2.9.0-alpha07 حاوی این commit ها است.

سازگاری چند پلتفرمی Kotlin

  • Lifecycle ViewModel SavedState اکنون با KMP سازگار است. این به شما امکان می دهد از SavedStateHandle در کدهای رایج استفاده کنید. ( Ib6394 , b/334076622 )

پشتیبانی از سریال سازی KotlinX

  • با پشتیبانی از KotlinX Serialization که در SavedState 1.3.0-alpha05 اضافه شده است، saved معرفی کرده‌ایم، یک نماینده ویژگی تنبل، تا ذخیره کلاس‌های @Serializable در یک SavedStateHandle را آسان کنیم و آن کلاس‌ها به طور خودکار در طول فرآیند مرگ و بازیابی بازیابی شوند. لطفاً توجه داشته باشید که نماینده saved تنبل است و تا زمانی که به آن دسترسی پیدا نشود، init lambda را صدا نمی کند یا چیزی را در SavedStateHandle ذخیره نمی کند. ( I47a88 , b/376026744 )

    @Serializable
    data class Person(val firstName: String, val lastName: String)
    
    class MyViewModel(handle: SavedStateHandle) : ViewModel() {
        var person by handle.saved { Person("John", "Doe") }
    
        fun onPersonChanged(person: Person) {
            this.person = person
        }
    }
    

تغییرات API

  • برای بازگشت یک MutableStateFlow getMutableStateFlow به SavedStateHandle اضافه کنید. این تابع جدید انحصاری کلید است و با getLiveData قابل استفاده نیست. اگر سعی کنید از هر دو برای دسترسی به یک حالت استفاده کنید، یک استثنا ایجاد می شود. ( I04a4f , b/375408415 )

نسخه 2.9.0-alpha06

30 اکتبر 2024

androidx.lifecycle:lifecycle-*:2.9.0-alpha06 منتشر شد. نسخه 2.9.0-alpha06 حاوی این commit ها است.

تغییرات رفتار

  • حالت Lifecycle.DESTROYED پایانه است و هر تلاشی برای انتقال یک Lifecycle از آن به هر حالت دیگری اکنون منجر به یک IllegalStateException می شود. ( I116c4 , b/370577987 )
  • SavedStateHandle دیگر شامل SavedStateProvider.saveState() نمی شود که در آن Bundle بازگشتی خالی باشد. ( I910b5 , b/370577987 )

رفع اشکال

  • Lifecycle.eventFlow اکنون با DESTROYED Lifecycle به درستی کامل می شود ( I293b2 , b/374043130 )

نسخه 2.9.0-alpha05

16 اکتبر 2024

androidx.lifecycle:lifecycle-*:2.9.0-alpha05 بدون هیچ تغییر قابل توجهی منتشر شد. نسخه 2.9.0-alpha05 حاوی این commit ها است.

نسخه 2.9.0-alpha04

2 اکتبر 2024

androidx.lifecycle:lifecycle-*:2.9.0-alpha04 منتشر شد. نسخه 2.9.0-alpha04 حاوی این commit ها است.

چند پلتفرم کاتلین

  • ماژول lifecycle-viewmodel-savedstate اکنون برای سازگاری با KMP برای آماده‌سازی APIهایی مانند SavedStateHandle که در مجموعه منبع مشترک در نسخه‌های آینده در دسترس قرار می‌گیرند، پیکربندی شده است. ( I503ed , I48764 , b/334076622 )

نسخه 2.9.0-alpha03

18 سپتامبر 2024

androidx.lifecycle:lifecycle-*:2.9.0-alpha03 منتشر شد. نسخه 2.9.0-alpha03 حاوی این commit ها است.

رفع اشکال

  • از Lifecycle 2.8.6 : خطای NullSafeMutableLiveData Lint پشتیبانی از پخش هوشمند را بهبود بخشیده است و از مثبت کاذب جلوگیری می کند. ( 85fed6 , b/181042665 )

به روز رسانی های وابستگی

نسخه 2.9.0-alpha02

4 سپتامبر 2024

androidx.lifecycle:lifecycle-*:2.9.0-alpha02 منتشر شد. نسخه 2.9.0-alpha02 حاوی این commit ها است.

رفع اشکال

  • از Lifecycle 2.8.5 : قوانین androidx.lifecycle.ReportFragment ProGuard را به روز کنید تا مبهم سازی امکان پذیر باشد. ( ff898e1 )

مشارکت خارجی

  • androidx.compose.ui.platform.LocalLifecycleOwner به مجموعه منبع مشترک (KMP) منتقل کنید. با تشکر از ایوان ماتکوف از JetBrains برای کمک. ( 8cd5d03 )
  • از Lifecycle 2.8.5 : نماینده پسوند SavedStateHandle.saveable اکنون از مقادیر nullable پشتیبانی می کند. با تشکر از رومن کالوکیویچ برای مشارکت. ( 0d78ea6 )

نسخه 2.9.0-alpha01

7 آگوست 2024

androidx.lifecycle:lifecycle-*:2.9.0-alpha01 منتشر شد. نسخه 2.9.0-alpha01 حاوی این commit ها است.

چند پلتفرم کاتلین

  • lifecycle-testing اکنون با KMP سازگار است. ( Iea41e )
  • افزودن پشتیبانی برای هدف چند پلتفرمی linuxArm64 kotlin ( I139d3 , b/338268719 )

ویژگی های جدید

  • یک مصنوع KMP جدید androidx.lifecycle:lifecycle-viewmodel-testing موجود است که یک کلاس ViewModelScenario برای آزمایش ViewModels به صورت مجزا، با پشتیبانی از onCleared (همه پلتفرم‌ها) و SavedStateHandle (فقط اندروید) ارائه می‌کند. ( 337f68d , c9b3409 , 9799a95c , b/264602919 )
  • ایجاد ViewModel با ViewModelProvider در حال حاضر موضوعی امن است. حاشیه نویسی @MainThread حذف شده است. ( Ifd978 , b/237006831 )

تغییرات API

  • تابع کارخانه CreationExtras.Key() را اضافه کنید تا ایجاد اشیاء CreationExtras.Key ناشناس را ساده کنید. ( I970ee )
  • CreationExtras اکنون شامل اضافه بارهای اپراتور شبیه به نقشه برای فعال کردن دستکاری اصطلاحی محتوا در Kotlin است. این اجازه می دهد تا از in ، += ، و + با CreationExtras استفاده کنید. ( Ib4353 )
  • اکنون CreationExtras متدهای equals ، hashCode و toString را پیاده سازی می کند. ( Ib4353 )
  • NewInstanceFactory اکنون در JVM Desktop و اهداف Android در دسترس است. ( d3d0892 )
  • ویژگی افزونه درون خطی برای نمایش امن برنامه زیربنایی در زبان Kotlin نسخه 2.0 ( I39df2 )

رفع اشکال

  • طرح دستی دسترسی به APIهای پلتفرم جدید حذف شد زیرا این به طور خودکار از طریق مدلسازی API هنگام استفاده از R8 با AGP 7.3 یا جدیدتر (مثلا R8 نسخه 3.3) و برای همه ساخت‌ها هنگام استفاده از AGP 8.1 یا بالاتر (به عنوان مثال D8 نسخه 8.1) اتفاق می‌افتد. به مشتریانی که از AGP استفاده نمی‌کنند، توصیه می‌شود به نسخه 8.1 یا بالاتر D8 به‌روزرسانی کنند. برای جزئیات بیشتر به این مقاله مراجعه کنید. ( If6b4c , b/345472586 )

نسخه 2.8

نسخه 2.8.7

30 اکتبر 2024

androidx.lifecycle:lifecycle-*:2.8.7 منتشر شد. نسخه 2.8.7 شامل این commit ها است.

تغییرات API

  • androidx.compose.ui.platform.LocalLifecycleOwner اکنون در مجموعه منبع مشترک (KMP) در دسترس است. ( 6a3f5b3 )
  • lifecycle-runtime-compose : مصنوعات desktop حذف شدند و مصنوعات -jvmStubs و -linuxx64Stubs اضافه شدند. هیچ یک از این اهداف قرار نیست مورد استفاده قرار گیرند، آنها مکان هایی هستند که به تلاش های Jetbrains Compose کمک می کنند. ( 6a3f5b3 )

نسخه 2.8.6

18 سپتامبر 2024

androidx.lifecycle:lifecycle-*:2.8.6 منتشر شد. نسخه 2.8.6 حاوی این commit ها است.

رفع اشکال

  • خطای NullSafeMutableLiveData Lint پشتیبانی از پخش هوشمند را بهبود بخشیده است و از مثبت کاذب جلوگیری می کند. ( 85fed6 , b/181042665 )

به روز رسانی های وابستگی

نسخه 2.8.5

4 سپتامبر 2024

androidx.lifecycle:lifecycle-*:2.8.5 منتشر شد. نسخه 2.8.5 حاوی این commit ها است.

رفع اشکال

  • قوانین androidx.lifecycle.ReportFragment ProGuard را به روز کنید تا مبهم سازی امکان پذیر باشد. ( ff898e1 )

مشارکت خارجی

  • نماینده پسوند SavedStateHandle.saveable اکنون از مقادیر nullable پشتیبانی می کند. با تشکر از رومن کالوکیویچ برای مشارکت. ( 0d78ea6 )

نسخه 2.8.4

24 جولای 2024

androidx.lifecycle:lifecycle-*:2.8.4 منتشر شد. نسخه 2.8.4 حاوی این commit ها است.

رفع اشکال

  • اکنون LiveData.asFlow() به درستی مواردی را که در آن Flow برگشتی بلافاصله پس از دریافت مقداری که قبلاً در LiveData تنظیم شده است تکمیل می شود (به عنوان مثال، هنگام استفاده take(1) ). ( I9c566 )
  • Lifecycle*Effect اکنون بی‌توان است (به عنوان مثال، اگر onStopOrDispose به دلیل توقف چرخه حیات فراخوانی شده باشد، برای بار دوم پس از دور ریختن فراخوانی نمی‌شود مگر اینکه چرخه حیات دوباره به حالت STARTED برگردد). ( I5f607 , b/352364595 )

نسخه 2.8.3

1 ژوئیه 2024

androidx.lifecycle:lifecycle-*:2.8.3 منتشر شد. نسخه 2.8.3 شامل این commit ها است.

رفع اشکال

  • مشکل سازگاری رو به عقب Lifecycle 2.8 با Compose 1.6.0 و پایین تر هنگام استفاده از کوچک کردن کد برطرف شد. ( aosp/3133056 , b/346808608 )

نسخه 2.8.2

12 ژوئن 2024

androidx.lifecycle:lifecycle-*:2.8.2 منتشر شد. نسخه 2.8.2 شامل این commit ها است.

رفع اشکال

  • رفع مشکل CompositionLocal LocalLifecycleOwner not present - اکنون می‌توانید از Lifecycle 2.8.2 با هر نسخه از Compose بدون هیچ راه‌حلی استفاده کنید. ( aosp/3105647 , b/336842920 )
  • هنگام مخلوط کردن نسخه‌های قبلی وابستگی‌های compileOnly Lifecycle با نسخه‌های 2.8+، ViewModelProvider دیگر خراب نمی‌شود و مشکلات کتابخانه‌هایی مانند LeakCanary را برطرف می‌کند. ( I80383 , b/341792251 )

نسخه 2.8.1

29 مه 2024

androidx.lifecycle:lifecycle-*:2.8.1 منتشر شد. نسخه 2.8.1 حاوی این commit ها است.

رفع اشکال

  • lifecycle-viewmodel-compose اکنون فقط یک وابستگی مشترک به compose-runtime دارد و وابستگی رایج آن به compose-ui حذف می‌شود. مصنوع اندرویدی compose-ui خود را برای سازگاری حفظ می کند. ( aosp/3079334 , b/339562627 )
  • ادغام saveable ViewModel با استفاده از نمایندگان ویژگی، اکنون از نام کلاس به عنوان بخشی از کلید تولید شده خودکار استفاده می کند، و اگر چندین کلاس SavedStateHandle یکسان استفاده کنند، از تداخل جلوگیری می کند. ( aosp/3063463 )

نسخه 2.8.0

14 مه 2024

androidx.lifecycle:lifecycle-*:2.8.0 منتشر شد. نسخه 2.8.0 شامل این commit ها است.

تغییرات مهم از 2.7.0

  • LocalLifecycleOwner از Compose UI به lifecycle-runtime-compose منتقل شده است تا APIهای کمکی مبتنی بر Compose آن بتوانند خارج از Compose UI استفاده شوند.
  • مصنوع lifecycle-runtime-compose اکنون حاوی APIهای dropUnlessResumed و dropUnlessStarted است که به شما امکان می‌دهد کلیک یا رویدادهای دیگری را که حتی پس از کاهش LifecycleOwner به زیر Lifecycle.State انجام می‌شوند، رها کنید. به عنوان مثال، این می تواند با Navigation Compose برای جلوگیری از مدیریت رویدادهای کلیک پس از اینکه انتقال به صفحه دیگری از قبل شروع شده است استفاده شود: onClick: () -> Unit = dropUnlessResumed { navController.navigate(NEW_SCREEN) }
  • ViewModel.viewModelScope اکنون یک پارامتر سازنده قابل لغو است که به شما امکان می دهد توزیع کننده و SupervisorJob() خود را تزریق کنید یا با استفاده از backgroundScope موجود در runTest پیش فرض را لغو کنید. ( I2817c , b/264598574 )

    class MyViewModel(
      // Make Dispatchers.Main the default, rather than Dispatchers.Main.immediate
      viewModelScope: CoroutineScope = Dispatchers.Main + SupervisorJob()
    ) : ViewModel(viewModelScope) {
      // Use viewModelScope as before, without any code changes
    }
    
    // Allows overriding the viewModelScope in a test
    fun Test() = runTest {
      val viewModel = MyViewModel(backgroundScope)
    }
    
  • ViewModel در Kotlin بازنویسی شده است و اکنون از AutoClosable به جای Closeable استفاده می کند. اکنون از افزودن اشیای AutoCloseable با key پشتیبانی می کند که امکان بازیابی آنها را از طریق getCloseable() فراهم می کند.

  • فراخوانی LifecycleStartEffect و LifecycleResumeEffect بدون کلید اکنون یک خطا است و از همان قرارداد DisposableEffect API پیروی می کند که این APIها منعکس می کنند.

  • LiveDataReactiveStreams.toPublisher(lifecycleOwner, liveData) به نفع LiveData.toPublisher(lifecycleOwner) منسوخ شد.

  • پسوندهای lifecycle-livedata-core-ktx kotlin اکنون به ماژول lifecycle-livedata-core منتقل شده‌اند.

  • NullSafeMutableLiveData برای جلوگیری از بسیاری از موارد مثبت کاذب مجدداً اصلاح شده است.

سازگاری چند پلتفرمی کاتلین چرخه حیات

APIهای اصلی Lifecycle در Lifecycle ، LifecycleOwner ، LifecycleObserver ، Lifecycle.State ، Lifecycle.Event ، و LifecycleRegistry اکنون در مصنوعات سازگار با Kotlin Multiplatform ارسال می شوند.

مصنوعات تحت تاثیر:

  • lifecycle-common اکثر API ها را به common منتقل می کند و علاوه بر اندروید از jvm و iOS نیز پشتیبانی می کند.
  • lifecycle-runtime اکثر APIها را به common منتقل می کند و علاوه بر اندروید از jvm و iOS نیز پشتیبانی می کند.
  • lifecycle-runtime-ktx اکنون خالی است و همه API ها به lifecycle-runtime منتقل می شوند.
  • lifecycle-runtime-compose همه APIها را به common منتقل می‌کند و یک مصنوع Android را ارسال می‌کند که با پشتیبانی چند پلتفرمی androidx.compose مطابقت دارد.

سازگاری چند پلتفرمی ViewModel Kotlin

مصنوع مدل lifecycle-viewmodel و APIهایی مانند ViewModel ، ViewModelStore ، ViewModelStoreOwner ، و ViewModelProvider اکنون در مصنوعات سازگار با Kotlin Multiplatform ارسال می شوند.

برای تطبیق با این تغییر، روش‌هایی مانند روش‌هایی در ViewModelProvider که java.lang.Class<T> انتخاب کردند، اکنون یک متد معادل دارند که یک kotlin.reflect.KClass<T> می‌گیرد.

سازگاری باینری در Android حفظ شده است، اما در مقایسه سطح API Android با سطح API مشترک، چند تغییر قابل توجه وجود دارد:

  • ساختن یک نمونه ViewModelProvider اکنون از طریق متدهای ViewModelProvider.create() انجام می شود نه اینکه مستقیما سازنده آن را فراخوانی کند.
  • ViewModelProvider.NewInstanceFactory و ViewModelProvider.AndroidViewModelFactory فقط در Android در دسترس هستند.
    • سفارشی Factories توصیه می شود از ViewModelProvider.Factory گسترش یافته و از روش create استفاده کنند که یک CreationExtras می گیرد یا از viewModelFactory Kotlin DSL استفاده می کند.
  • استفاده از ViewModelProvider بدون کارخانه سفارشی در پلتفرم‌های غیر JVM منجر به UnsupportedOperationException می‌شود. در پلتفرم‌های JVM، اگر کارخانه سفارشی ارائه نشود، سازگاری با استفاده از سازنده ViewModel بدون args حفظ می‌شود.
  • viewModelScope به یک EmptyCoroutineContext در پلتفرم هایی که Dispatchers.Main در دسترس نیست (مثلاً لینوکس) بازگشتی خواهد داشت.

مصنوعات تحت تاثیر:

  • lifecycle-viewmodel اکثر API ها را به common منتقل می کند و علاوه بر اندروید از jvm و iOS نیز پشتیبانی می کند.
  • lifecycle-viewmodel-ktx اکنون خالی است و همه APIها به lifecycle-viewmodel منتقل می شوند.
  • lifecycle-viewmodel-compose همه APIها را به common منتقل می‌کند و یک مصنوع Android را ارسال می‌کند، که با پشتیبانی چند پلتفرمی androidx.compose مطابقت دارد.

تغییرات رفتار

  • InitializerViewModelFactory (شامل تابع سازنده viewModelFactory ) اکنون یک IllegalArgumentException ایجاد می کند اگر یک initializer با همان clazz: KClass<VM : ViewModel> قبلاً اضافه شده باشد. ( IC3a36 )

مسائل شناخته شده

نسخه 2.8.0-rc01

1 مه 2024

androidx.lifecycle:lifecycle-*:2.8.0-rc01 منتشر شد. نسخه 2.8.0-rc01 حاوی این commit ها است.

رفع اشکال

  • مشکلی که در آن نمایه خط پایه برای کلاس‌های lifecycle-common به درستی بسته‌بندی نشده بود، برطرف شد. اینها اکنون در AAR lifecycle-runtime بسته بندی شده اند. ( aosp/3038274 , b/322382422 )
  • اصلاح یک تغییر سفارش ناخواسته در نحوه پاک شدن نمونه های AutoCloseable متصل به ViewModel - ترتیب قبلی addCloseable(String, AutoCloseable) و سپس addClosable(AutoCloseable) و سپس onCleared() بازیابی شده است. ( aosp/3041632 )
  • رفتار ایجاد پیش فرض را برای viewModelScope برای محیط های Native و JVM Desktop بهبود دهید. ( aosp/3039221 )

مشارکت خارجی

  • با تشکر از Victor Kropp برای بهبود بررسی موضوع اصلی در JVM Desktop. ( aosp/3037116 )

نسخه 2.8.0-beta01

17 آوریل 2024

androidx.lifecycle:lifecycle-*:2.8.0-beta01 منتشر شد. نسخه 2.8.0-beta01 حاوی این تعهدات است.

ویژگی های جدید

  • مصنوع lifecycle-runtime-compose اکنون با Kotlin Multiplatform سازگار است، کد آن را به common منتقل می‌کند و یک مصنوع Android را ارسال می‌کند، مطابق با پشتیبانی از multiplatform برای androidx.compose . ( If7a71 , I4f4a0 , b/331769623 )

نسخه 2.8.0-alpha04

3 آوریل 2024

androidx.lifecycle:lifecycle-*:2.8.0-alpha04 منتشر شد. نسخه 2.8.0-alpha04 حاوی این commit ها است.

ویژگی های جدید

  • مصنوع lifecycle-viewmodel-compose اکنون با Kotlin Multiplatform سازگار است، کد آن را به common منتقل می‌کند و یک مصنوع Android را ارسال می‌کند، که با پشتیبانی چندپلتفرمی androidx.compose مطابقت دارد. برای تطبیق با این تغییر، روش Composable viewModel اکنون یک KClass علاوه بر یک java.lang.Class می پذیرد. ( b/330323282 )

رفع اشکال

  • NullSafeMutableLiveData برای جلوگیری از بسیاری از موارد مثبت کاذب مجدداً اصلاح شده است. ( I2d8c1 ، Iafb18 ، I03463 ، I7ecef )

به روز رسانی وابستگی

  • مصنوع lifecycle-viewmodel-compose اکنون به Compose 1.6.0 بستگی دارد.
  • چرخه حیات اکنون به Profile Installer 1.3.1 بستگی دارد.

نسخه 2.8.0-alpha03

20 مارس 2024

androidx.lifecycle:lifecycle-*:2.8.0-alpha03 منتشر شد. نسخه 2.8.0-alpha03 حاوی این commit ها است.

ویژگی های جدید

  • ViewModel.viewModelScope اکنون یک پارامتر سازنده قابل لغو است که به شما امکان می دهد توزیع کننده و SupervisorJob() خود را تزریق کنید یا با استفاده از backgroundScope موجود در runTest پیش فرض را لغو کنید. ( I2817c , b/264598574 )

    class MyViewModel(
      // Make Dispatchers.Main the default, rather than Dispatchers.Main.immediate
      viewModelScope: CoroutineScope = Dispatchers.Main + SupervisorJob()
    ) : ViewModel(viewModelScope) {
      // Use viewModelScope as before, without any code changes
    }
    
    // Allows overriding the viewModelScope in a test
    fun Test() = runTest {
      val viewModel = MyViewModel(backgroundScope)
    }
    

سازگاری چند پلتفرمی Kotlin

مصنوع مدل lifecycle-viewmodel و APIهایی مانند ViewModel ، ViewModelStore ، ViewModelStoreOwner ، و ViewModelProvider اکنون در مصنوعات سازگار با Kotlin Multiplatform ارسال می شوند. ( b/214568825 )

برای تطبیق با این تغییر، روش‌هایی مانند روش‌هایی در ViewModelProvider که java.lang.Class<T> انتخاب کردند، اکنون یک متد معادل دارند که یک kotlin.reflect.KClass<T> می‌گیرد.

سازگاری باینری در Android حفظ شده است، اما در مقایسه سطح API Android با سطح API مشترک، چند تغییر قابل توجه وجود دارد:

  • ساختن یک نمونه ViewModelProvider اکنون از طریق متدهای ViewModelProvider.create() انجام می شود نه اینکه مستقیما سازنده آن را فراخوانی کند.
  • ViewModelProvider.NewInstanceFactory و ViewModelProvider.AndroidViewModelFactory فقط در Android در دسترس هستند.
    • سفارشی Factories توصیه می شود از ViewModelProvider.Factory گسترش یافته و از روش create استفاده کنند که یک CreationExtras می گیرد یا از viewModelFactory Kotlin DSL استفاده می کند.
  • استفاده از ViewModelProvider بدون کارخانه سفارشی در پلتفرم‌های غیر JVM منجر به UnsupportedOperationException می‌شود. در پلتفرم‌های JVM، اگر کارخانه سفارشی ارائه نشود، سازگاری با استفاده از سازنده ViewModel بدون args حفظ می‌شود.
  • viewModelScope به یک EmptyCoroutineContext در پلتفرم هایی که Dispatchers.Main در دسترس نیست (مثلاً لینوکس) بازگشتی خواهد داشت.

تغییرات رفتار

  • InitializerViewModelFactory (شامل تابع سازنده viewModelFactory ) اکنون یک IllegalArgumentException ایجاد می کند اگر یک initializer با همان clazz: KClass<VM : ViewModel> قبلاً اضافه شده باشد. ( IC3a36 )

رفع اشکال

  • ViewModel.getCloseable اکنون کلیدهای تکراری را کنترل می کند: اگر key قبلاً دارای یک منبع AutoCloseable مرتبط با آن باشد، منبع قدیمی جایگزین شده و بلافاصله بسته می شود. ( IBeb67 )
  • دسترسی به viewModelScope یک ViewModel در حال حاضر امن است. ( If4766 , b/322407038 )

مشارکت خارجی

  • LocalLifecycleOwner از Compose UI به lifecycle-runtime-compose منتقل شد تا API های کمکی مبتنی بر Compose آن بتوانند خارج از Compose UI استفاده شوند. با تشکر از جیک وارتون برای مشارکت. ( I6c41b , b/328263448 )

نسخه 2.8.0-alpha02

21 فوریه 2024

androidx.lifecycle:lifecycle-*:2.8.0-alpha02 منتشر شد. نسخه 2.8.0-alpha02 حاوی این commit ها است.

ویژگی های جدید

  • API‌های dropUnlessResumed و dropUnlessStarted اضافه شده‌اند که به شما امکان می‌دهند کلیک یا رویدادهای دیگری را که حتی پس از سقوط LifecycleOwner به زیر Lifecycle.State رخ می‌دهند، رها کنید. به عنوان مثال، این می تواند با Navigation Compose برای جلوگیری از مدیریت رویدادهای کلیک پس از اینکه انتقال به صفحه دیگری از قبل شروع شده است استفاده شود: onClick: () -> Unit = dropUnlessResumed { navController.navigate(NEW_SCREEN) } ( Icba83 , b/317230685 )

تبدیل کاتلین

  • ViewModel اکنون در Kotlin نوشته شده است ( I16f26 , b/214568825 )
  • پسوندهای lifecycle-viewmodel-ktx kotlin اکنون به ماژول چرخه حیات پایه منتقل شده‌اند. ( Id787b , b/274800183 )
  • الحاقات lifecycle-runtime-ktx kotlin اکنون به ماژول چرخه حیات پایه منتقل شده اند. ( IC3686 , b/274800183 )
  • پسوندهای lifecycle-livedata-core-ktx kotlin اکنون به ماژول چرخه حیات پایه منتقل شده‌اند. ( I54a3d , b/274800183 )

سازگاری چند پلتفرمی Kotlin

  • APIهای اصلی Lifecycle در Lifecycle ، LifecycleOwner ، LifecycleObserver ، Lifecycle.State ، Lifecycle.Event ، و LifecycleRegistry اکنون در مصنوعات سازگار با Kotlin Multiplatform ارسال می شوند. ( b/317249252 )

تغییرات API

  • فراخوانی LifecycleStartEffect و LifecycleResumeEffect بدون کلید اکنون یک خطا است و از همان قرارداد DisposableEffect API پیروی می کند که این APIها منعکس می کنند. ( Ib0e0c , b/323518079 )
  • ViewModel اکنون از AutoCloseable به جای Closeable استفاده می کند. این یک تغییر سازگار با عقب است. ( I27f8e , b/214568825 )
  • LiveDataReactiveStreams.toPublisher(lifecycleOwner, liveData) به نفع LiveData.toPublisher(lifecycleOwner) منسوخ شد. ( Iabe29 , b/262623005 )

مشارکت خارجی

  • از ایوان ماتکوف از Jetbrains برای کمک به انتقال Lifecycle به چند پلتفرم Kotlin تشکر می کنیم. ( aosp/2926690 ، I0c5ac ، If445d )

نسخه 2.8.0-alpha01

24 ژانویه 2024

androidx.lifecycle:lifecycle-*:2.8.0-alpha01 منتشر شد. نسخه 2.8.0-alpha01 حاوی این commit ها است.

ویژگی های جدید

  • ViewModel اکنون از افزودن اشیای Closeable با key پشتیبانی می کند که امکان بازیابی آنها را از طریق getCloseable() فراهم می کند. ( I3cf63 )

نسخه 2.7

نسخه 2.7.0

10 ژانویه 2024

androidx.lifecycle:lifecycle-*:2.7.0 منتشر شد. نسخه 2.7.0 حاوی این commit ها است.

تغییرات مهم از 2.6.0

  • TestLifecycleOwner اکنون یک تابع تعلیق setCurrentState() شامل می شود که تضمین می کند که تغییر حالت و تمام تماس های LifecycleObserver قبل از بازگشت کامل می شود. قابل‌توجه، برخلاف تنظیم مستقیم ویژگی currentState ، این ویژگی runBlocking استفاده نمی‌کند و استفاده از آن را در برنامه‌های معمولی مانند آنچه توسط runTest ارائه می‌کند، ایمن می‌سازد.
  • پسوندهای LiveData map و switchMap اکنون رفتار distinctUntilChanged را منعکس می‌کنند - اگر LiveData دارای value تنظیم شده باشد، تابع map / switchMap فوراً برای پر کردن value LiveData برگشتی فراخوانی می‌شود. This ensures that the initial value will be set as part of the first composition (when used with observeAsState() ), but does not change the observation behavior - updates values from the source LiveData will still only apply once you start observing the LiveData .
  • This release fixes an issue where SavedStateHandle would not properly restore custom Parcelable classes after process death and recreation. Due to type information that is lost by the Android framework, arrays of custom Parcelables require additional work (manually creating a typed array of the right type) and the documentation on get , getLiveData , and getStateFlow now specifically calls this limitation out.
  • The proguard keep rules associated with LifecycleObserver have been removed. This means that proguarded code that wishes to use APIs via reflection (such as using the long since deprecated @OnLifecycleEvent annotation) will need to provide their own keep rules for their specific use case.

Lifecycle Event Observability

  • As an alternative to using a LifecycleEventObserver , you can now observe a Flow of Lifecycle.Event via the Lifecycle.asFlow() extension method.
  • Jetpack Compose users can now use LifecycleEventEffect to run Compose side effects based on Lifecycle.Event .
@Composable
fun HomeScreen(viewModel: HomeViewModel = viewModel()) {
  LifecycleEventEffect(Lifecycle.Event.ON_RESUME) {
    viewModel.refreshData()
  }
  // …
}
  • Jetpack Compose users can use LifecycleStartEffect and LifecycleResumeEffect to handle pairs of events - started to stopped and resumed to paused, respectively. This API mirrors the one found in DisposableEffect and is suitable for cases where the change being made when the state is going up needs to be reversed when going back down.
fun HomeScreen(viewModel: HomeViewModel = viewModel()) {
  LifecycleStartEffect(viewModel) {
    val timeTracking = viewModel.startTrackingTimeOnScreen()
    onStopOrDispose {
      timeTracking.stopTrackingTimeOnScreen()
    }
  }
  // …
}

See Run code on lifecycle events for more information.

Lifecycle State Observability

  • The current Lifecycle.State can now be observed via the Lifecycle.currentStateFlow property, which returns a StateFlow where the value is the current Lifecycle.State .
  • Jetpack Compose users can use the Lifecycle.currentStateAsState() extension to directly expose Lifecycle.State as Compose State . This is equivalent (and a shorter alternative) to lifecycle.currentStateFlow.collectAsState() .

See Collect lifecycle state with flows for more information.

Version 2.7.0-rc02

13 دسامبر 2023

androidx.lifecycle:lifecycle-*:2.7.0-rc02 is released. Version 2.7.0-rc02 contains these commits.

رفع اشکال

  • Fixed an issue where SavedStateHandle would not properly restore custom Parcelable classes after process death and recreation. Due to type information that is lost by the Android framework, arrays of custom Parcelables require additional work (manually creating a typed array of the right type) and the documentation on get , getLiveData , and getStateFlow now specifically calls this limitation out. ( I0b55a )

Version 2.7.0-rc01

15 نوامبر 2023

androidx.lifecycle:lifecycle-*:2.7.0-rc01 is released. Version 2.7.0-rc01 contains these commits.

رفع اشکال

  • LifecycleStartEffect and LifecycleResumeEffect now correctly dispose and recreate the effect block if the LifecycleOwner is changed. ( Ia25c6 )

Version 2.7.0-beta01

1 نوامبر 2023

androidx.lifecycle:lifecycle-*:2.7.0-beta01 is released with no changes. Version 2.7.0-beta01 contains these commits.

  • A beta version bump, no major changes to this release version.

Version 2.7.0-alpha03

18 اکتبر 2023

androidx.lifecycle:lifecycle-*:2.7.0-alpha03 is released. Version 2.7.0-alpha03 contains these commits.

ویژگی های جدید

  • lifecycle-runtime-testing now contains a new Lint check to avoid setting the Lifecycle.State of the TestLifecycleOwner by using the currentState field when inside of a coroutine. The Lint check now suggests the suspending setCurrentState which allows setting the Lifecycle.State without blocking. ( Icf728 , b/297880630 )

رفع اشکال

  • Fixed an issue with LiveData.switchMap where returning the same LiveData instance both on the initial call and a subsequent call would prevent the LiveData instance from being added as a source. ( Ibedcba7 )

Version 2.7.0-alpha02

6 سپتامبر 2023

androidx.lifecycle:lifecycle-*:2.7.0-alpha02 is released. Version 2.7.0-alpha02 contains these commits.

ویژگی های جدید

  • TestLifecycleOwner now includes the suspending function setCurrentState() to give users the option of using TestLifecycleOwner from within a coroutine such as one provided by runTest . ( I329de , b/259344129 )

API Changes

  • All files from the lifecycle-livedata-ktx modules have been moved into the main lifecycle-livedata module. ( I10c6f , b/274800183 )

تغییرات رفتار

  • The LiveData.map() and LiveData.switchMap() extensions now sets the value of the returned LiveData if the previous LiveData has had a value set on it, ensuring that using the resulting LiveData in Jetpack Compose has the right state on the initial composition. ( I91d2b , b/269479952 )
  • ViewModel 's addCloseable() now immediately closes the Closeable if the ViewModel has already received a call to onCleared() . ( I4712e , b/280294730 )

رفع اشکال

  • From Lifecycle 2.6.2 : Fixed an issue where SavedStateHandle would not correctly be restored after process death if the state was restored, save() was called without actually saving the state in the parent SavedStateRegistry , and then the state was restored again. This fixes the interaction between rememberSaveable and Navigation Compose's NavHost . ( aosp/2729289 )

Version 2.7.0-alpha01

26 جولای 2023

androidx.lifecycle:lifecycle-*:2.7.0-alpha01 is released. Version 2.7.0-alpha01 contains these commits.

API Changes

  • Lifecycle.State is now Compose-observable via Lifecycle.currentStateFlow , which returns a StateFlow where the value is the current Lifecycle.State . ( Ib212d , b/209684871 )
  • Lifecycle.Event s can now able to be observed as a Flow with Lifecycle.asFlow(). ( If2c0f , b/176311030 )
  • LifecycleResumeEffect API has been added to run Compose SideEffect s based on both Lifecycle.Event.ON_RESUME and Lifecycle.Event.ON_PAUSE event callbacks. ( I60386 , b/235529345 )
  • LifecycleStartEffect API has been added to run Compose SideEffect s based on Lifecycle.Event.ON_START and Lifecycle.Event.ON_STOP event callbacks. ( I5a8d1 , b/235529345 )
  • LifecycleEventEffect API has been added to run Compose SideEffect s based on Lifecycle.Event . ( Ic9794 , b/235529345 )
  • Lifecycle.collectAsState() extension has been added to directly expose Lifecycle.State as Compose State . This is equivalent (and a shorter alternative) to lifecycle.currentStateFlow.collectAsState() . ( I11015 , b/235529345 )

رفع اشکال

  • The LiveData.distinctUntilChanged() extension now sets the value of the returned LiveData if the previous LiveData has had a value set on it. This does not change the observation behavior - updated values from the source LiveData will still only apply once you start observing the LiveData returned from distinctUntilChanged() . ( Ib482f )
  • The proguard keep rules associated with LifecycleObserver have been removed. This means that proguarded code that wishes to use APIs via reflection will need to provide their own keep rules for their specific use case. ( Ia12fd )

نسخه 2.6

Version 2.6.2

6 سپتامبر 2023

androidx.lifecycle:lifecycle-*:2.6.2 is released. Version 2.6.2 contains these commits.

رفع اشکال

  • Fixed an issue where SavedStateHandle would not correctly be restored after process death if the state was restored, save() was called without actually saving the state in the parent SavedStateRegistry , and then the state was restored again. This fixes the interaction between rememberSaveable and Navigation Compose's NavHost . ( aosp/2729289 )

Version 2.6.1

March 22, 2023

androidx.lifecycle:lifecycle-*:2.6.1 is released. Version 2.6.1 contains these commits.

به روز رسانی های وابستگی

نسخه 2.6.0

8 مارس 2023

androidx.lifecycle:lifecycle-*:2.6.0 is released. Version 2.6.0 contains these commits.

Important changes since 2.5.0

  • LiveData now includes a new isInitialized property that indicates whether an explicit value has ever been set on the LiveData , allowing you to distinguish between liveData.value returning null because no value has ever been set or an explicit null value.
  • MediatorLiveData now includes a constructor to set an initial value.
  • Added a new extension on StateFlow and Flow of collectAsStateWithLifecycle() that collect from flows and represents its latest value as Compose State in a lifecycle-aware manner.
  • Lifecycle.launchWhenX methods and Lifecycle.whenX methods have been deprecated as the use of a pausing dispatcher can lead to wasted resources in some cases. It is recommended to use Lifecycle.repeatOnLifecycle . For more information about one-time suspending work, please see this explanation on why this is inherently unsafe.
  • Kotlin Conversion - A large number of Lifecycle classes have been converted to Kotlin. All converted classes still retain their binary compatibility with previous versions. The following classes have source incompatible changes for classes written in Kotlin: ViewTreeLifecycleOwner , LiveDataReactiveStreams , HasDefaultViewModelProviderFactory , ViewTreeViewModelStoreOwner , Transformations , ViewModelStoreOwner , LifecycleOwner

The table below provides the source conversions for the new version of lifecycle.

Lifecycle 2.5 Lifecycle 2.5 (KTX) Lifecycle 2.6
Transformations.switchMap(liveData) {...} liveData.switchMap {...} liveData.switchMap {...}
Transformations.map(liveData) {...} liveData.map {...} liveData.map {...}
Transformations.distinctUntilChanged(liveData) {...} liveData.distinctUntilChanged{...} liveData.distinctUntilChanged{...}
LiveDataReactiveStreams.fromPublisher(publisher) publisher.toLiveData() publisher.toLiveData()
LiveDataReactiveStreams.toPublisher(lifecycleOwner, liveData) liveData.toPublisher(lifecycleOwner) liveData.toPublisher(lifecycleOwner)
override fun getDefaultViewModelProviderFactory(): ViewModelProvider.Factory = factory override fun getDefaultViewModelProviderFactory(): ViewModelProvider.Factory = factory override val defaultViewModelProviderFactory = factory
override fun getDefaultViewModelCreationExtras(): CreationExtras = extras override fun getDefaultViewModelCreationExtras(): CreationExtras = extras override val defaultViewModelProviderCreationExtras = extras
ViewTreeLifecycleOwner.set(view, owner) ViewTreeLifecycleOwner.set(view, owner) view.setViewTreeLifecycleOwner(owner)
ViewTreeLifecycleOwner.get(view) view.findViewTreeLifecycleOwner() view.findViewTreeLifecycleOwner()
override fun getViewModelStore(): ViewModelStore = store override fun getViewModelStore(): ViewModelStore = store override val viewModelStore: ViewModelStore = store
override fun getLifecycle(): Lifecycle = registry override fun getLifecycle(): Lifecycle = registry override val lifecycle: Lifecycle get() = registry
  • The nullability of the onChanged method of a Observer created in Kotlin now matches the nullability of the generic type. If you want Observer.onChanged() to accept a nullable type, you must instantiate the Observer with a nullable type.
  • These classes were also converted to Kotlin, but remain source compatible: DefaultLifecycleObserver , LifecycleEventObserver , Lifecycle , LifecycleRegistry , LifecycleObserver , ViewModelStore , AndroidViewModel , AbstractSavedStateViewModelFactory , LifecycleService , ServiceLifecycleDispatcher , and ProcessLifecycleOwner

Version 2.6.0-rc01

22 فوریه 2023

androidx.lifecycle:lifecycle-*:2.6.0-rc01 is released. Version 2.6.0-rc01 contains these commits.

رفع اشکال

  • The LiveData.distinctUntilChanged() extension now sets the value of the returned LiveData if the previous LiveData has had a value set on it. This does not change the observation behavior - updated values from the source LiveData will still only apply once you start observing the LiveData returned from distinctUntilChanged() . ( Ib482f )

Version 2.6.0-beta01

8 فوریه 2023

androidx.lifecycle:lifecycle-*:2.6.0-beta01 is released. Version 2.6.0-beta01 contains these commits.

Kotlin Conversions

  • LifecycleOwner is now written in Kotlin. This is a source incompatible change for classes written in Kotlin - they must now override the lifecycle property rather than implementing the previous getLifecycle() function. ( I75b4b , b/240298691 )
  • ViewModelStoreOwner is now in Kotlin. This is a source incompatible change for classes written in Kotlin - they must now override the viewModelStore property rather than implementing the previous getViewModelStore() function. ( I86409 , b/240298691 )
  • The Kotlin extension on LifecycleOwner that provides the lifecycleScope field has been moved to the lifecycle-common artifact from lifecycle-runtime-ktx . ( I41d78 , b/240298691 )
  • The Kotlin extension on Lifecycle that provides the coroutineScope field has been moved to the lifecycle-common artifact from lifecycle-runtime-ktx . ( Iabb91 , b/240298691 )

Version 2.6.0-alpha05

25 ژانویه 2023

androidx.lifecycle:lifecycle-*:2.6.0-alpha05 is released. Version 2.6.0-alpha05 contains these commits.

Kotlin Conversions

  • Transformations is now written in Kotlin. This is a source incompatible change for those classes written in Kotlin that were directly using syntax such as Transformations.map - Kotlin code must now use the Kotlin extension method syntax that was previously only available when using lifecycle-livedata-ktx . When using the Java programming language, the versions of these methods that take an androidx.arch.core.util.Function method are deprecated and replaced with the versions that take a Kotlin Function1 . This change maintains binary compatibility. ( I8e14f )
  • ViewTreeViewModelStoreOwner is now written in Kotlin. This is a source incompatible change for those classes written in Kotlin - you must now directly import and use the Kotlin extension methods on View of androidx.lifecycle.setViewTreeViewModelStoreOwner and androidx.lifecycle.findViewTreeViewModelStoreOwner to set and find a previously set owner. This is binary compatible and remains source compatible for implementations written in the Java programming language. ( Ia06d8 , Ib22d8 , b/240298691 )
  • The HasDefaultViewModelProviderFactory interface is now written in Kotlin. This is a source incompatible change for classes written in Kotlin - they must now override the defaultViewModelProviderFactory and defaultViewModelCreationExtras properties rather than implementing the previous corresponding functions. ( Iaed9c , b/240298691 )
  • Observer is now written in Kotlin. Its onChanged() method now uses the name value for its parameter. ( Iffef2 , I4995e , b/240298691 )
  • AndroidViewModel , AbstractSavedStateViewModelFactory , LifecycleService , ServiceLifecycleDispatcher , and ProcessLifecycleOwner are now written in Kotlin ( I2e771 , Ibae40 , I160d7 , I08884 , I1cda7 , b/240298691 )

Version 2.6.0-alpha04

11 ژانویه 2023

androidx.lifecycle:lifecycle-*:2.6.0-alpha04 is released. Version 2.6.0-alpha04 contains these commits.

ویژگی های جدید

  • LiveData now includes a new isInitialized property that indicates whether an explicit value has ever been set on the LiveData , allowing you to distinguish between liveData.value returning null because no value has ever been set or an explicit null value. ( Ibd018 )

API Changes

  • The collectAsStateWithLifecycle() APIs of lifecycle-runtime-compose are no longer in experimental status. ( I09d42 , b/258835424 )
  • Lifecycle.launchWhenX methods and Lifecycle.whenX methods have been deprecated as the use of a pausing dispatcher can lead to wasted resources in some cases. It is recommended to use Lifecycle.repeatOnLifecycle . ( Iafc54 , b/248302832 )

Kotlin Conversions

  • ViewTreeLifecycleOwner is now written in Kotlin. This is a source incompatible change for those classes written in Kotlin - you must now directly import and use the Kotlin extension methods on View of androidx.lifecycle.setViewTreeLifecycleOwner and androidx.lifecycle.findViewTreeLifecycleOwner to set and find a previously set owner. This replaces the previous Kotlin extension in lifecycle-runtime-ktx . This is binary compatible and remains source compatible for implementations written in the Java programming language. ( I8a77a , I5234e , b/240298691 )
  • LiveDataReactiveStreams is now written in Kotlin. The Kotlin extensions previously in lifecycle-reactivestreams-ktx have been moved into the lifecycle-reactivestreams module and have become the primary surface for code written in Kotlin. This is a source incompatible change for code written in Kotlin if you were not already using the Kotlin extension method APIs. ( I2b1b9 , I95d22 , b/240298691 )
  • DefaultLifecycleObserver , LifecycleEventObserver , Lifecycle , LifecycleRegistry , LifecycleObserver , and ViewModelStore are now written in Kotlin ( Iadffd , ( I60034 , I8c52c , I9593d , I01fe1 , I59a23 , b/240298691 )

رفع اشکال

  • SavedStateHandle no longer crashes with a ClassCastException when calling get() with the incorrect class type. ( I6ae7c )

Version 2.6.0-alpha03

24 اکتبر 2022

androidx.lifecycle:lifecycle-*:2.6.0-alpha03 is released. Version 2.6.0-alpha03 contains these commits.

رفع اشکال

  • Fixed an issue with constraints between different Lifecycle modules not working as intended. ( I18d0d , b/249686765 )
  • Errors thrown by LifecycleRegistry.moveToState() now include a more helpful error messaging that informs developers of the component causing the error. ( Idf4b2 , b/244910446 )

Version 2.6.0-alpha02

7 سپتامبر 2022

androidx.lifecycle:lifecycle-*:2.6.0-alpha02 is released. Version 2.6.0-alpha02 contains these commits.

API Changes

  • MediatorLiveData now includes a constructor to set an initial value. ( Ib6cc5 , b/151244085 )

رفع اشکال

  • Lifecycle artifacts now include constraints that ensure that all inter-dependent Lifecycle artifacts use the same version, automatically upgrading other dependencies when one is upgraded. b/242871265
  • FlowLiveData.asFlow() now creates a callbackFlow rather than using its own Channel implementation to ensure thread-safety and context preservation. ( I4a8b2 , b/200596935 )
  • FlowLiveData 's asLiveData function will now preserve the initial value of a StateFlow when creating the new LiveData object. ( I3f530 , b/157380488 )
  • From Lifecycle 2.5.1 : Custom implementations of AndroidViewModelFactory now correctly calls the create(modelClass) function when using the stateful constructor with Lifecycle 2.4+ ( I5b315 , b/238011621 )

Version 2.6.0-alpha01

29 ژوئن 2022

androidx.lifecycle:lifecycle-*:2.6.0-alpha01 is released. Version 2.6.0-alpha01 contains these commits.

ویژگی های جدید

  • Added a new extension on StateFlow and Flow of collectAsStateWithLifecycle that collect from flows and represents its latest value as Compose State in a lifecycle-aware manner. The flow is collected and the new emission is set to the State's value when the lifecycle is at least in a certain Lifecycle.State . When the lifecycle falls below that Lifecycle.State , the flow collection stops and the State's value is not updated. ( I1856e , b/230557927 )

نسخه 2.5

نسخه 2.5.1

27 جولای 2022

androidx.lifecycle:lifecycle-*:2.5.1 is released. Version 2.5.1 contains these commits.

رفع اشکال

  • Custom implementations of AndroidViewModelFactory now correctly call the create(modelClass) function when using the stateful AndroidViewModelFactory constructor with Lifecycle 2.4+. ( I5b315 , b/238011621 )

نسخه 2.5.0

29 ژوئن 2022

androidx.lifecycle:lifecycle-*:2.5.0 is released. Version 2.5.0 contains these commits.

Important changes since 2.4.0

  • SavedStateHandle now offers a getStateFlow() API that returns a Kotlin StateFlow for monitoring value changes as an alternative to using LiveData .

  • ViewModel CreationExtras - when writing a custom ViewModelProvider.Factory , it is no longer required to extend AndroidViewModelFactory or AbstractSavedStateViewModelFactory to gain access to an Application or SavedStateHandle , respectively. Instead, these fields are provided to every ViewModelProvider.Factory subclass as CreationExtras via the new overload of create : create(Class<T>, CreationExtras) . These extras are provided automatically by your Activity or Fragment when using Activity 1.5.0 and Fragment 1.5.0 , respectively.

    class CustomFactory : ViewModelProvider.Factory {
        override fun <T : ViewModel> create(modelClass: Class<T>, extras: CreationExtras): T {
            return when (modelClass) {
                HomeViewModel::class -> {
                    // Get the Application object from extras
                    val application = checkNotNull(extras[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY])
                    // Pass it directly to HomeViewModel
                    HomeViewModel(application)
                }
                DetailViewModel::class -> {
                    // Create a SavedStateHandle for this ViewModel from extras
                    val savedStateHandle = extras.createSavedStateHandle()
                    DetailViewModel(savedStateHandle)
                }
                else -> throw IllegalArgumentException("Unknown class $modelClass")
            } as T
        }
    }
    
  • lifecycle-viewmodel now provides a viewModelFactory Kotlin DSL that allows you define your ViewModelProvider.Factory in terms of one or more lambda initializers, one for each particular ViewModel class your custom factory supports, using CreationExtras as the primary data source.

    val customFactory = viewModelFactory {
        // The return type of the lambda automatically sets what class this lambda handles
        initializer {
            // Get the Application object from extras provided to the lambda
            val application = checkNotNull(get(ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY))
            HomeViewModel(application)
        }
        initializer {
            val savedStateHandle = createSavedStateHandle()
            DetailViewModel(savedStateHandle)
        }
    }
    
  • lifecycle-viewmodel-compose now offers a viewModel() API that takes a lambda factory for creating a ViewModel instance without requiring the creation of a custom ViewModelProvider.Factory .

    // Within a @Composable, you can now skip writing a custom Factory
    // and instead write a lambda to do the initialization of your ViewModel
    val detailViewModel = viewModel {
      // This lambda is only called the first time the ViewModel is created
      // and all CreationExtras are available inside the lambda
      val savedStateHandle = createSavedStateHandle()
      DetailViewModel(savedStateHandle)
    }
    
  • SavedStateHandle Compose Saver Integration - the lifecycle-viewmodel-compose artifact now contains new experimental APIs in SavedStateHandle.saveable that allow rememberSaveable like behavior backed by the SavedStateHandle of a `ViewModel.

    class ListScreenViewModel(handle: SavedStateHandle): ViewModel() {
        // This value survives both configuration changes and process death and recreation
        val editMode by handle.saveable { mutableStateOf(false) }
    }
    
  • Added an addCloseable() API and a new constructor overload that allow you to add one or more Closeable objects to the ViewModel that will be closed when the ViewModel is cleared without requiring any manual work in onCleared() .

    For instance, to create a coroutine scope that you can inject into a ViewModel, but control via testing, you can create a CoroutineScope that implements Closeable :

    class CloseableCoroutineScope(
        context: CoroutineContext = SupervisorJob() + Dispatchers.Main.immediate
    ) : Closeable, CoroutineScope {
        override val coroutineContext: CoroutineContext = context
        override fun close() {
            coroutineContext.cancel()
       }
    }
    

    Which can then be used in your ViewModel constructor while maintaining the same lifetime as viewModelScope :

    class TestScopeViewModel(
        val customScope: CloseableCoroutineScope = CloseableCoroutineScope()
    ) : ViewModel(customScope) {
        // You can now use customScope in the same way as viewModelScope
    }
    

تغییر رفتار

  • Attempting to move the Lifecycle.State from INITIALIZED to DESTROYED will now always throw an IllegalStateException regardless of whether the Lifecycle has an attached observer.
  • LifecycleRegistry will now clear their observers when they reach the DESTROYED state.

Version 2.5.0-rc02

15 ژوئن 2022

androidx.lifecycle:lifecycle-*:2.5.0-rc02 is released. Version 2.5.0-rc02 contains these commits.

رفع اشکال

  • ViewModelProvider will no longer crash when mixing previous versions of compileOnly Lifecycle dependencies with versions 2.5+. ( I81a66 , b/230454566 )

Version 2.5.0-rc01

11 مه 2022

androidx.lifecycle:lifecycle-*:2.5.0-rc01 is released. Version 2.5.0-rc01 contains these commits.

رفع اشکال

  • MediatorLiveData.addSource() now throws a NullPointerException when passed a null source instead of propagating the null source to observers.( Ibd0fb , b/123085232 )

Version 2.5.0-beta01

20 آوریل 2022

androidx.lifecycle:lifecycle-*:2.5.0-beta01 is released. Version 2.5.0-beta01 contains these commits.

API Changes

  • Added SavedStateHandle.saveable property delegates to use property names as keys for persisting state into the SavedStateHandle ( I8bb86 , b/225014345 )

رفع اشکال

  • Fixed an issue where nesting one NavHost within another NavHost in a non-primary bottom navigation tab would lead to an IllegalStateException when using multiple back stacks. ( I11bd5 , b/228865698 )

Version 2.5.0-alpha06

6 آوریل 2022

androidx.lifecycle:lifecycle-*:2.5.0-alpha06 is released. Version 2.5.0-alpha06 contains these commits.

ویژگی های جدید

  • Add experimental MutableState overload to SavedStateHandle.saveable for parity with rememberSaveable ( I38cfe , b/224565154 )

API Changes

  • CreationExtras is now abstract instead of sealed. ( Ib8a7a )

رفع اشکال

  • Fixed an IllegalStateException: Already attached to lifecycleOwner error caused by SavedStateHandleController . ( I7ea47 , b/215406268 )

Version 2.5.0-alpha05

23 مارس 2022

androidx.lifecycle:lifecycle-*:2.5.0-alpha05 is released. Version 2.5.0-alpha05 contains these commits.

ویژگی های جدید

  • The lifecycle-viewmodel-compose module now provides SavedStateHandleSaver , an experimental API that ensures values in a SavedStateHandle are integrated correctly with the same saved instance state that rememberSaveable uses. ( Ia88b7 , b/195689777 )

API Changes

  • Fixed a compatibility issue with Lifecycle 2.3 and newer Lifecycle versions in Java. ( I52c8a , b/219545060 )

رفع اشکال

  • SavedStateViewFactory now supports using CreationExtras even when it was initialized with a SavedStateRegistryOwner . If extras are provided, the initialized arguments are ignored. ( I6c43b , b/224844583 )

Version 2.5.0-alpha04

9 مارس 2022

androidx.lifecycle:lifecycle-*:2.5.0-alpha04 is released. Version 2.5.0-alpha04 contains these commits.

API Changes

  • SavedStateHandle now offers a getStateFlow() API that returns a Kotlin StateFlow for monitoring value changes as an alternative to using LiveData . ( Iad3ab , b/178037961 )

Version 2.5.0-alpha03

23 فوریه 2022

androidx.lifecycle:lifecycle-*:2.5.0-alpha03 is released. Version 2.5.0-alpha03 contains these commits.

ویژگی های جدید

  • Added an addCloseable() API and a new constructor overload that allow you to add one or more Closeable objects to the ViewModel that will be closed when the ViewModel is cleared without requiring any manual work in onCleared() . ( I55ea0 )
  • lifecycle-viewmodel now provides an InitializerViewModelFactory that allows you to add lambda for handling particular ViewModel classes, using CreationExtras as the primary data source. ( If58fc , b/216687549 )
  • lifecycle-viewmodel-compose now offers a viewModel() API that takes a lambda factory for creating a ViewModel instance without requiring the creation of a custom ViewModelProvider.Factory . ( I97fbb , b/216688927 )

API Changes

  • You can now create a ViewModel with CreationExtras via lifecycle-viewmodel-compose . ( I08887 , b/216688927 )

تغییر رفتار

  • Attempting to move the Lifecycle.State from INITIALIZED to DESTROYED will now always throw an IllegalStateException regardless of whether the Lifecycle has an attached observer. ( I7c390 , b/177924329 )
  • LifecycleRegistry will now clear their observers when they reach the DESTROYED state. ( I4f8dd , b/142925860 )

Version 2.5.0-alpha02

9 فوریه 2022

androidx.lifecycle:lifecycle-*:2.5.0-alpha02 is released. Version 2.5.0-alpha02 contains these commits.

API Changes

  • SavedStateHandle and SavedStateViewModelFactory have been converted to Kotlin. This has improved the nullability of the generics in both classes. ( Ib6ce2 , b/216168263 , I9647a , b/177667711 )
  • The LiveData switchMap function parameter can now have a nullable output. ( I40396 , b/132923666 )
  • The LiveData -ktx extensions are now annotated with @CheckResult to enforce that the result is used when calling these functions. ( Ia0f05 , b/207325134 )

تغییر رفتار

  • SavedStateHandle now properly stores the defaultValue when no value for the specified key exists. ( I1c6ce , b/178510877 )

رفع اشکال

  • From Lifecycle 2.4.1 : Updated lifecycle-process to depend on Startup 1.1.1 to ensure that fixes that prevent ProcessLifecycleInitializer from throwing a StartupException are available by default. ( Ib01df , b/216490724 )
  • There is now an improved error message when custom AndroidViewModel classes have parameters in the wrong order and attempt to create a ViewModel . ( I340f7 , b/177667711 )
  • You can now create a view model via CreationExtras using the AndroidViewModelFactory without setting an application. ( I6ebef , b/217271656 )

Version 2.5.0-alpha01

26 ژانویه 2022

androidx.lifecycle:lifecycle-*:2.5.0-alpha01 is released. Version 2.5.0-alpha01 contains these commits.

ViewModel CreationExtras

With this release, we are laying the groundwork for restructuring how a ViewModel is constructed. Instead of a rigid set of subclasses of ViewModelProvider.Factory that each add additional functionality (allowing an Application constructor parameter via AndroidViewModelFactory , allowing a SavedStateHandle constructor parameter via SavedStateViewModelFactory and AbstractSavedStateViewModelFactory , etc.), we are moving to a world of stateless factories that rely on a new concept, CreationExtras . ( Ia7343 , b/188691010 , b/188541057 )

With this change, ViewModelProvider no longer makes direct calls into the previous create(Class<T>) method of ViewModelProvider.Factory . Instead, it calls into a new overload of create : create(Class<T>, CreationExtras) . This means that any direct implementation of the ViewModelProvider.Factory instance now has access to each of these new CreationExtras :

  • ViewModelProvider.NewInstanceFactory.VIEW_MODEL_KEY : this String provides access to the custom key you passed to ViewModelProvider.get() .
  • ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY provides access to the Application class.
  • SavedStateHandleSupport.SAVED_STATE_REGISTRY_OWNER_KEY provides access to the SavedStateRegistryOwner that is being used to construct this ViewModel.
  • SavedStateHandleSupport.VIEW_MODEL_STORE_OWNER_KEY provides access to the ViewModelStoreOwner that is being used to construct this ViewModel.
  • SavedStateHandleSupport.DEFAULT_ARGS_KEY provides access to the Bundle of arguments that should be used to construct a SavedStateHandle .

These extras are provided by default when using Activity 1.5.0-alpha01 , Fragment 1.5.0-alpha01 , and Navigation 2.5.0-alpha01 . If you use an earlier version of those libraries, your CreationExtras will be empty - all of the existing subclasses of ViewModelProvider.Factory have been rewritten to support both the legacy creation path used by earlier versions of those libraries and the CreationExtras path which will be used going forward.

These CreationExtras allow you to construct a ViewModelProvider.Factory that passes just the information you need to each ViewModel without relying on a strict hierarchy of Factory subclasses:

class CustomFactory : ViewModelProvider.Factory {
    override fun <T : ViewModel> create(modelClass: Class<T>, extras: CreationExtras): T {
        return when (modelClass) {
            HomeViewModel::class -> {
                // Get the Application object from extras
                val application = checkNotNull(extras[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY])
                // Pass it directly to HomeViewModel
                HomeViewModel(application)
            }
            DetailViewModel::class -> {
                // Create a SavedStateHandle for this ViewModel from extras
                val savedStateHandle = extras.createSavedStateHandle()
                DetailViewModel(savedStateHandle)
            }
            else -> throw IllegalArgumentException("Unknown class $modelClass")
        } as T
    }
}

We use the createSavedStateHandle() Kotlin extension function on CreationExtras from SavedStateHandleSupport to construct a SavedStateHandle only for the one ViewModel that needs it. ( Ia6654 , b/188541057 )

Custom CreationExtras can be provided by overriding getDefaultViewModelCreationExtras() in your ComponentActivity or Fragment , thus making them available to your custom ViewModelProvider.Factory as a built in form of assisted injection. These extras will automatically be made available to your custom Factory when used directly with ViewModelProvider or when using the by viewModels() and by activityViewModels() Kotlin property extensions. ( I79f2b , b/207012584 , b/207012585 , b/207012490 )

رفع اشکال

  • Fixed an issue where the default value provided to a SavedStateHandle would reappear after process death and recreation, even if it was specifically removed from the SavedStateHandle . As a consequence of this, SavedStateHandle will no longer merge default values and restored values together, instead only using the restored values as the source of truth. ( I53a4b )

نسخه 2.4

نسخه 2.4.1

9 فوریه 2022

androidx.lifecycle:lifecycle-*:2.4.1 is released. Version 2.4.1 contains these commits.

رفع اشکال

  • Backported from Lifecycle 2.5.0-alpha01 : Fixed an issue where the default value provided to a SavedStateHandle would re-appear after process death and recreation, even if it was specifically removed from the SavedStateHandle . As a consequence of this, SavedStateHandle will no longer merge default values and restored values together, instead only using the restored values as the source of truth. ( I53a4b )
  • lifecycle-process now depends on Androidx Startup 1.1.1 which fixed a regression in where using ProcessLifecycleInitializer would cause an StartupException . ( b/216490724 )

نسخه 2.4.0

27 اکتبر 2021

androidx.lifecycle:lifecycle-*:2.4.0 is released. Version 2.4.0 contains these commits.

Important changes since 2.3.0

  • @OnLifecycleEvent was deprecated. LifecycleEventObserver or DefaultLifecycleObserver should be used instead.
  • androidx.lifecycle:lifecycle-viewmodel-compose library was added. It provides viewModel() composable and LocalViewModelStoreOwner .
    • Source-breaking change : ViewModelProvider has been rewritten in Kotlin. ViewModelProvider.Factory.create method no longer allows nullable generic.
  • New coroutines API were added to androidx.lifecycle:lifecycle-runtime-ktx :
  • Lifecycle.repeatOnLifecycle , API that executes a block of code in a coroutine when the Lifecycle is at least in a certain state. The block will cancel and re-launch as the lifecycle moves in and out of the target state;
  • Flow.flowWithLifecycle , API that emits values from the upstream Flow when the lifecycle is at least in a certain state.
  • DefaultLifecycleObserver was moved from lifecycle.lifecycle-common-java8 to lifecycle.lifecycle-common . lifecycle.lifecycle-common-java8 doesn't provide anymore any additional functionality on top of lifecycle.lifecycle-common , so dependency on it can be replaced by lifecycle.lifecycle-common .
  • Non coroutines API from lifecycle-viewmodel-ktx have been moved to the lifecycle-viewmodel module.
  • lifecycle-process now uses androidx.startup to initialize the ProcessLifecycleOwner .

    Previously, this was being done by androidx.lifecycle.ProcessLifecycleOwnerInitializer .

    If you used tools:node="remove" the ContentProvider being used to initialize process lifecycle in the past, then you need to do the following instead.

     <provider
        android:name="androidx.startup.InitializationProvider"
        android:authorities=\"${applicationId}.androidx-startup"
        android:exported="false"
        tools:node=\"merge">
        <!-- If you are using androidx.startup to initialize other components -->
        <meta-data
            android:name="androidx.lifecycle.ProcessLifecycleInitializer"
            android:value="androidx.startup"
            tools:node="remove" />
     </provider>
    

    (یا)

     <!-- If you want to disable androidx.startup completely. -->
     <provider
        android:name="androidx.startup.InitializationProvider"
        android:authorities="${applicationId}.androidx-startup"
        tools:node="remove">
     </provider>
    

Version 2.4.0-rc01

29 سپتامبر 2021

androidx.lifecycle:lifecycle-*:2.4.0-rc01 is released with no changes from Lifecycle 2.4.0-beta01. Version 2.4.0-rc01 contains these commits.

Version 2.4.0-beta01

15 سپتامبر 2021

androidx.lifecycle:lifecycle-*:2.4.0-beta01 is released. Version 2.4.0-beta01 contains these commits.

API Changes

  • @OnLifecycleEvent was deprecated. LifecycleEventObserver or DefaultLifecycleObserver should be used instead. ( I5a8fa )
  • DefaultLifecycleObserver was moved from androidx.lifecycle.lifecycle-common-java8 to androidx.lifecycle.lifecycle-common . androidx.lifecycle.lifecycle-common-java8 doesn't provide anymore any additional functionality on top of androidx.lifecycle.lifecycle-common , so dependency on it can be replaced by androidx.lifecycle.lifecycle-common . ( I021aa )
  • Non coroutines API from lifecycle-viewmodel-ktx have been moved to the lifecycle-viewmodel module. ( I6d5b2 )

External Contribution

  • Thanks dmitrilc for fixing a type in the ViewModel documentation! ( #221 )

Version 2.4.0-alpha03

4 آگوست 2021

androidx.lifecycle:lifecycle-*:2.4.0-alpha03 is released. Version 2.4.0-alpha03 contains these commits.

API Changes

  • Source-breaking change : ViewModelProvider has been rewritten in Kotlin. ViewModelProvider.Factory.create method no longer allows nullable generic. ( I9b9f6 )

تغییرات رفتار

  • The Lifecycle.repeatOnLifecycle : block is now always invoked serially when repeating execution. ( Ibab33 )

External Contribution

  • Thanks chao2zhang for fixing the code snippets in the repeatOnLifecycle documentation. #205 .

Version 2.4.0-alpha02

16 ژوئن 2021

androidx.lifecycle:lifecycle-*:2.4.0-alpha02 is released. Version 2.4.0-alpha02 contains these commits.

ویژگی های جدید

  • Added a new RepeatOnLifecycleWrongUsage lint check to lifecycle-runtime-ktx that detects when repeateOnLifecycle is incorrectly used in onStart() or onResume() . ( 706078 , b/187887400 )

API Changes

  • The LifecycleOwner.addRepeatingJob API is removed in favor of Lifecycle.repeatOnLifecycle that respects structured concurrency and is easier to reason about. ( I4a3a8 )
  • Make ProcessLifecycleInitializer public so other androidx.startup.Initializer s can use these as dependencies. ( I94c31 )

رفع اشکال

  • Fixed an issue with the NullSafeMutableLiveData lint check when the field has modifiers. ( #147 , b/183696616 )
  • Fixed another issue with the NullSafeMutableLiveData lint check when using generics. ( #161 , b/184830263 )

External Contribution

Version 2.4.0-alpha01

24 مارس 2021

androidx.lifecycle:lifecycle-*:2.4.0-alpha01 is released. Version 2.4.0-alpha01 contains these commits.

تغییرات رفتار

  • lifecycle-process now uses androidx.startup to initialize the ProcessLifecycleOwner .

    Previously, this was being done by androidx.lifecycle.ProcessLifecycleOwnerInitializer .

    If you used tools:node="remove" the ContentProvider being used to initialize process lifecycle in the past, then you need to do the following instead.

     <provider
        android:name="androidx.startup.InitializationProvider"
        android:authorities=\"${applicationId}.androidx-startup"
        android:exported="false"
        tools:node=\"merge">
        <!-- If you are using androidx.startup to initialize other components -->
        <meta-data
            android:name="androidx.lifecycle.ProcessLifecycleInitializer"
            android:value="androidx.startup"
            tools:node="remove" />
     </provider>
    

    (یا)

     <!-- If you want to disable androidx.startup completely. -->
     <provider
        android:name="androidx.startup.InitializationProvider"
        android:authorities="${applicationId}.androidx-startup"
        tools:node="remove">
     </provider>
    

API Changes

  • Added a Flow.flowWithLifecycle API that emits values from the upstream Flow when the lifecycle is at least in a certain state using the Lifecycle.repeatOnLifecycle API. This is an alternative to the also new LifecycleOwner.addRepeatinJob API. ( I0f4cd )

رفع اشکال

  • From Lifecycle 2.3.1 : The NonNullableMutableLiveData lint rule can now properly differentiate between field variables with different nullability. ( b/169249668 )

Lifecycle Viewmodel Compose Version 1.0.0

Version 1.0.0-alpha07

16 ژوئن 2021

androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha07 is released. Version 1.0.0-alpha07 contains these commits.

Breaking API Changes

  • viewModel() now takes an optional ViewModelStoreOwner , making it easier to work with owners other than the LocalViewModelStoreOwner . For example, you can now use viewModel(navBackStackEntry) to retrieve a ViewModel associated with a particular navigation graph. ( I2628d , b/188693123 )

Version 1.0.0-alpha06

2 ژوئن 2021

androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha06 is released. Version 1.0.0-alpha06 contains these commits.

Updated to be compatible with Compose version 1.0.0-beta08 .

Version 1.0.0-alpha05

18 مه 2021

androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha05 is released. Version 1.0.0-alpha05 contains these commits.

ویژگی های جدید

  • Updated to be compatible with Compose version 1.0.0-beta07 .

رفع اشکال

  • The AndroidManifest files from ui-test-manifest and ui-tooling-data are now compatible with Android 12 ( I6f9de , b/184718994 )

Version 1.0.0-alpha04

7 آوریل 2021

androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha04 is released. Version 1.0.0-alpha04 contains these commits.

Dependency Changes

  • This version allows androidx.hilt:hilt-navigation-compose and androidx.navigation:navigation-compose to sync dependencies on androidx.compose.compiler:compiler:1.0.0-beta04 and androidx.compose.runtime:runtime:1.0.0-beta04 . For 1.0.0, it is required that the compiler and runtime match.

Version 1.0.0-alpha03

10 مارس 2021

androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha03 is released. Version 1.0.0-alpha03 contains these commits.

API Changes

  • LocalViewModelStoreOwner.current now returns a nullable ViewModelStoreOwner to better determine whether a ViewModelStoreOwner is available in the current composition. APIs that require a ViewModelStoreOwner , such as viewModel() and NavHost , still throw an exception if a ViewModelStoreOwner is not set. ( Idf39a )

Lifecycle-Viewmodel-Compose Version 1.0.0-alpha02

24 فوریه 2021

androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha02 is released. Version 1.0.0-alpha02 contains these commits.

API Changes

  • LocalViewModelStoreOwner now has a provides functions that can be used with CompositionLocalProvider , replacing the asProvidableCompositionLocal() API. ( I45d24 )

Lifecycle-Viewmodel-Compose Version 1.0.0-alpha01

10 فوریه 2021

androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha01 is released. Version 1.0.0-alpha01 contains these commits.

ویژگی های جدید

  • The viewModel() composable and LocalViewModelStoreOwner were moved from androidx.compose.ui.viewinterop to this artifact in the androidx.lifecycle.viewmodel.compose package. ( I7a374 )

Version 2.3.1

Lifecycle Version 2.3.1

24 مارس 2021

androidx.lifecycle:lifecycle-*:2.3.1 is released. Version 2.3.1 contains these commits.

رفع اشکال

  • The NonNullableMutableLiveData lint rule can now properly differentiate between field variables with different nullability. ( b/169249668 )

نسخه 2.3.0

نسخه 2.3.0

10 فوریه 2021

androidx.lifecycle:lifecycle-*:2.3.0 is released. Version 2.3.0 contains these commits.

Major changes since 2.2.0

  • SavedStateHandle support for non-parcelable classes : SavedStateHandle now supports lazy serialization by allowing you to call setSavedStateProvider() for a given key, providing a SavedStateProvider that will get a callback to saveState() when the SavedStateHandle is asked to save its state. See Saving non-parcelable classes .
  • Lifecycle Behavior Enforcement :
    • LifecycleRegistry now enforces DESTROYED as a terminal state.
    • LifecycleRegistry now verifies that its methods are called on main thread. It was always a requirement for lifecycles of activities, fragments etc. An addition of observers from non-main threads resulted in hard to catch crashes in runtime. For LifecycleRegistry objects that owned by your own components, you can explicitly opt out from checks by using LifecycleRegistry.createUnsafe(...) , but then you have to ensure that a proper synchronization is in place when this LifecycleRegistry is accessed from different threads.
  • Lifecycle State and Event Helpers : Added static helper methods of downFrom(State) , downTo(State) , upFrom(State) , upTo(State) to Lifecycle.Event for generating the Event given a State and transition direction. Added the getTargetState() method that provides the State that the Lifecycle will transition to directly following the Event .
  • withStateAtLeast : Added Lifecycle.withStateAtLeast APIs that await a lifecycle state and run a non-suspending block of code synchronously at the point of state change, then resume with the result. These APIs differ from the existing when* methods as they do not permit running suspending code and do not employ a custom dispatcher. ( aosp/1326081 )
  • ViewTree APIs : A new ViewTreeLifecycleOwner.get(View) and ViewTreeViewModelStoreOwner.get(View) API allows you to retrieve the containing LifecycleOwner and ViewModelStoreOwner , respectively, given a View instance. You must upgrade to Activity 1.2.0 and Fragment 1.3.0 , and AppCompat 1.3.0-alpha01 or higher to populate this correctly. The findViewTreeLifecycleOwner and findViewTreeViewModelStoreOwner Kotlin extensions are available in lifecycle-runtime-ktx and lifecycle-viewmodel-ktx , respectively.
  • LiveData.observe() Kotlin extension deprecation : The LiveData.observe() Kotlin extension necessary to use lambda syntax is now deprecated as it is not necessary when using Kotlin 1.4.

Version 2.3.0-rc01

16 دسامبر 2020

androidx.lifecycle:lifecycle-*:2.3.0-rc01 is released. Version 2.3.0-rc01 contains these commits.

رفع اشکال

  • The keys() method of SavedStateHandle is now consistent before and after the state is saved - it now includes keys previously used with setSavedStateProvider() in addition to the keys used with set() and getLiveData() . ( aosp/1517919 , b/174713653 )

External Contribution

Version 2.3.0-beta01

1 اکتبر 2020

androidx.lifecycle:lifecycle-*:2.3.0-beta01 is released. Version 2.3.0-beta01 contains these commits.

API Changes

  • The LiveData.observe() Kotlin extension necessary to use lambda syntax is now deprecated as it is not necessary when using Kotlin 1.4. ( I40d3f )

رفع اشکال

Documentation Changes

  • The liveData builder and asLiveData() docs have been updated to include details about changing the given timeout values. ( aosp/1122324 )

Version 2.3.0-alpha07

19 آگوست 2020

androidx.lifecycle:lifecycle-*:2.3.0-alpha07 is released. Version 2.3.0-alpha07 contains these commits.

رفع اشکال

  • Fixed a crash issue in the NullSafeMutableLiveData Lint check. ( aosp/1395367 )

Version 2.3.0-alpha06

22 جولای 2020

androidx.lifecycle:lifecycle-*:2.3.0-alpha06 is released. Version 2.3.0-alpha06 contains these commits.

ویژگی های جدید

  • Added static helper methods of downFrom(State) , downTo(State) , upFrom(State) , upTo(State) to Lifecycle.Event for generating the Event given a State and transition direction. Added the getTargetState() method that provides the State that the Lifecycle will transition to directly following the Event . ( I00887 )
  • Added Lifecycle.withStateAtLeast APIs that await a lifecycle state and run a non-suspending block of code synchronously at the point of state change, then resume with the result. These APIs differ from the existing when* methods as they do not permit running suspending code and do not employ a custom dispatcher. ( aosp/1326081 )

تغییرات رفتار

  • LifecycleRegistry now enforces DESTROYED as a terminal state. ( I00887 )
  • LifecycleRegistry now verifies that its methods are called on main thread. It was always a requirement for lifecycles of activities, fragments etc. An addition of observers from non-main threads resulted in hard to catch crashes in runtime. For LifecycleRegistry objects that owned by your own components, you can explicitly opt out from checks by using LifecycleRegistry.createUnsafe(...) , but then you have to ensure that a proper synchronization is in place when this LifecycleRegistry is accessed from different threads ( Ie7280 , b/137392809 )

رفع اشکال

  • Fixed a crash in NullSafeMutableLiveData . ( b/159987480 )
  • Fixed an ObsoleteLintCustomCheck for Lint checks bundled with lifecycle-livedata-core-ktx (and specifically NullSafeMutableLiveData ). ( b/158699265 )

Version 2.3.0-alpha05

24 ژوئن 2020

androidx.lifecycle:lifecycle-*:2.3.0-alpha05 is released. Version 2.3.0-alpha05 contains these commits.

رفع اشکال

  • LiveData now better handles reentrant cases, avoiding duplicate calls to onActive() or onInactive() . ( b/157840298 )
  • Fixed an issue where Lint checks would not run when using Android Studio 4.1 Canary 6 or higher. ( aosp/1331903 )

Version 2.3.0-alpha04

10 ژوئن 2020

androidx.lifecycle:lifecycle-*:2.3.0-alpha04 is released. Version 2.3.0-alpha04 contains these commits.

رفع اشکال

  • Fixed a crash in the NonNullableMutableLiveData Lint check. ( b/157294666 )
  • The NonNullableMutableLiveData Lint check now covers significantly more cases where a null value was set on a MutableLiveData with a non-null type parameter. ( b/156002218 )

Version 2.3.0-alpha03

20 مه 2020

androidx.lifecycle:lifecycle-*:2.3.0-alpha03 are released. Version 2.3.0-alpha03 contains these commits.

ویژگی های جدید

  • SavedStateHandle now supports lazy serialization by allowing you to call setSavedStateProvider() for a given key, providing a SavedStateProvider that will get a callback to saveState() when the SavedStateHandle is asked to save its state. ( b/155106862 )
  • A new ViewTreeViewModelStoreOwner.get(View) API allows you to retrieve the containing ViewModelStoreOwner given a View instance. You must upgrade to Activity 1.2.0-alpha05 , Fragment 1.3.0-alpha05 , and AppCompat 1.3.0-alpha01 to populate this correctly. A findViewModelStoreOwner() Kotlin extension has been added to lifecycle-viewmodel-ktx . ( aosp/1295522 )

رفع اشکال

  • Fixed an issue that caused the MutableLiveData Lint checks released in Lifecycle 2.3.0-alpha01 from being published alongside the lifecycle-livedata-core-ktx artifact. ( b/155323109 )

Version 2.3.0-alpha02

29 آوریل 2020

androidx.lifecycle:lifecycle-*:2.3.0-alpha02 is released. Version 2.3.0-alpha02 contains these commits.

API Changes

  • SavedStateViewModelFactory now allows you to pass a null Application to its constructor to better support cases where one is not readily available and support for AndroidViewModel is not needed. ( aosp/1285740 )

رفع اشکال

  • Improved cold start performance by avoiding class verification failure on API 28 and lower devices. ( aosp/1282118 )

Version 2.3.0-alpha01

4 مارس 2020

androidx.lifecycle:lifecycle-*:2.3.0-alpha01 is released. Version 2.3.0-alpha01 contains these commits.

ویژگی های جدید

  • A new ViewTreeLifecycleOwner.get(View) API allows you to retrieve the containing LifecycleOwner given a View instance. You must upgrade to Activity 1.2.0-alpha01 and Fragment 1.3.0-alpha01 to populate this correctly. A findViewTreeLifecycleOwner Kotlin extension is available in lifecycle-runtime-ktx . ( aosp/1182361 , aosp/1182956 )
  • Added a new Lint check that warns you when setting a null value on a MutableLiveData that has been defined in Kotlin as non-null. This is available when using the livedata-core-ktx or livedata-ktx artifacts. ( aosp/1154723 , aosp/1159092 )
  • A new lifecycle-runtime-testing artifact is available that provides a TestLifecycleOwner that implements LifecycleOwner and provides a thread safe mutable Lifecycle . ( aosp/1242438 )

رفع اشکال

  • The lifecycle-runtime artifact now has a unique package name. ( aosp/1187196 )

Version 2.2.0

ViewModel-Savedstate Version 2.2.0

5 فوریه 2020

androidx.lifecycle:lifecycle-viewmodel-savedstate:2.2.0 is released. Version 2.2.0 contains these commits .

Lifecycle ViewModel SavedState now shares the same version as other Lifecycle artifacts. The behavior of 2.2.0 is identical to the behavior of 1.0.0 .

Version 2.2.0

22 ژانویه 2020

androidx.lifecycle:lifecycle-*:2.2.0 is released. Version 2.2.0 contains these commits .

Important changes since 2.1.0

  • Lifecycle Coroutine Integration : The new lifecycle-runtime-ktx artifact adds integration between Lifecycle and Kotlin coroutines. The lifecycle-livedata-ktx has also been expanded to take advantage of coroutines. See Use Kotlin coroutines with Architecture Components for more details.
  • ViewModelProviders.of() deprecation : ViewModelProviders.of() has been deprecated. You can pass a Fragment or FragmentActivity to the new ViewModelProvider(ViewModelStoreOwner) constructor to achieve the same functionality when using Fragment 1.2.0 .
  • lifecycle-extensions Artifact Deprecation : With the above deprecation of ViewModelProviders.of() , this release marks the deprecation of the last API in lifecycle-extensions and this artifact should now be considered deprecated in its entirety. We strongly recommend depending on the specific Lifecycle artifacts you need (such as lifecycle-service if you're using LifecycleService and lifecycle-process if you're using ProcessLifecycleOwner ) rather than lifecycle-extensions as there will not be a future 2.3.0 release of lifecycle-extensions .
  • Gradle Incremental Annotation Processor : Lifecycle's annotation processor is incremental by default. If your app is written in the Java 8 programming language you can use DefautLifecycleObserver instead; and if it's written in the Java 7 programming language you can use LifecycleEventObserver .

Version 2.2.0-rc03

4 دسامبر 2019

androidx.lifecycle:lifecycle-*:2.2.0-rc03 is released. Version 2.2.0-rc03 contains these commits .

رفع اشکال

  • Fixed a failure occurring when a mocked ViewModel was stored in ViewModelStore and queried later with default factory.
  • Fix a usage of Dispatchers.Main.immediate in launchWhenCreated and similar methods to be called synchronously during corresponding lifecycle event. ( aosp/1156203 )

External contributions

  • Thanks to Anders Järleberg for contributing the fix! ( aosp/1156203 )
  • Thanks to Vsevolod Tolstopyatov from Jetbrains for reviewing an implementation of inlined execution.

Dependency changes

  • Lifecycle Extensions now depends on Fragment 1.2.0-rc03 .

Version 2.2.0-rc02

7 نوامبر 2019

androidx.lifecycle:lifecycle-*:2.2.0-rc02 is released. Version 2.2.0-rc02 contains these commits .

رفع اشکال

  • Fixed a bug in the proguard setup of the library that affected devices running API 28+ if the target API is below 29. ( b/142778206 )

Version 2.2.0-rc01

23 اکتبر 2019

androidx.lifecycle:lifecycle-*:2.2.0-rc01 is released. Version 2.2.0-rc01 contains these commits .

رفع اشکال

  • Fixed an issue where launchWhenCreated and related methods would run one frame later than the associated lifecycle method due to its use of Dispatchers.Main instead of Dispatchers.Main.immediate . ( aosp/1145596 )

External contributions

  • Thanks to Nicklas Ansman for contributing the fix! ( aosp/1145596 )

Version 2.2.0-beta01

9 اکتبر 2019

androidx.lifecycle:lifecycle-*:2.2.0-beta01 is released. Version 2.2.0-beta01 contains these commits .

رفع اشکال

  • Fixed a regression introduced in Lifecycle 2.2.0-alpha05 in the ordering of ProcessLifecycleOwner and the activity's LifecycleOwner moving to started and resumed on Android 10 devices. ( aosp/1128132 )
  • Fixed a regression introduced in Lifecycle 2.2.0-alpha05 which would cause a NullPointerException when using version 2.0.0 or 2.1.0 of lifecycle-process . ( b/141536990 )

Version 2.2.0-alpha05

18 سپتامبر 2019

androidx.lifecycle:lifecycle-*:2.2.0-alpha05 is released. Version 2.2.0-alpha05 contains these commits .

رفع اشکال

  • Fixed a race condition in coroutine livedata builder. b/140249349

Version 2.2.0-alpha04

5 سپتامبر 2019

androidx.lifecycle:lifecycle-*:2.2.0-alpha04 is released. The commits included in this version can be found here .

ویژگی های جدید

  • lifecycleScope , whenCreated , whenStarted , whenResumed , viewModelScope , and the underlying implementation of liveData now use Dispatchers.Main.immediate instead of Dispatchers.Main . ( b/139740492 )

External contributions

  • Thanks to Nicklas Ansman for contributing the move to Dispatchers.Main.immediate ! ( aosp/1106073 )

Version 2.2.0-alpha03

7 آگوست 2019

androidx.lifecycle:lifecycle-*:2.2.0-alpha03 is released. The commits included in this version can be found here .

ویژگی های جدید

API تغییر می کند

  • ViewModelProviders.of() has been deprecated. You can pass a Fragment or FragmentActivity to the new ViewModelProvider(ViewModelStoreOwner) constructor to achieve the same functionality. ( aosp/1009889 )

Version 2.2.0-alpha02

2 جولای 2019

androidx.lifecycle:*:2.2.0-alpha02 is released. The commits included in this version can be found here .

API تغییر می کند

  • Replaced LiveDataScope.initialValue with LiveDataScope.latestValue which will track the current emitted value of the liveData block.
  • Added a new overload to the liveData builder that receives timeout parameter as type Duration

Version 2.2.0-alpha01

7 مه 2019

androidx.lifecycle:*:2.2.0-alpha01 is released. The commits included in this version can be found here .

ویژگی های جدید

  • This release adds new features that adds support for Kotlin coroutines for Lifecycle and LiveData. Detailed documentation on them can be found here .

ViewModel-SavedState Version 1.0.0

نسخه 1.0.0

22 ژانویه 2020

androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0 is released. Version 1.0.0 contains these commits .

Important features in 1.0.0

  • New SavedStateHandle class was added. It enables your ViewModel classes to access and to contribute to the saved state. This object can be received in constructor of ViewModel class and factories provided by default by Fragments and AppCompatActivity will inject SavedStateHandle automatically.
  • AbstractSavedStateViewModelFactory was added. It allows you to create custom factories for your ViewModel and provide them access to SavedStateHandle .

ViewModel-Savedstate Version 1.0.0-rc03

4 دسامبر 2019

androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-rc03 is released. Version 1.0.0-rc03 contains these commits .

Dependency changes

  • Lifecycle ViewModel SavedState now depends on Lifecycle 2.2.0-rc03 .

Viewmodel-Savedstate Version 1.0.0-rc02

7 نوامبر 2019

androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-rc02 is released. Version 1.0.0-rc02 contains these commits .

Dependency changes

  • Now depends on lifecycle 2.2.0-rc02 .

ViewModel-SavedState Version 1.0.0-rc01

23 اکتبر 2019

androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-rc01 is released with no changes from 1.0.0-beta01 . Version 1.0.0-rc01 contains these commits .

ViewModel-Savedstate Version 1.0.0-beta01

9 اکتبر 2019

androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-beta01 is released. Version 1.0.0-beta01 contains these commits .

رفع اشکال

  • Fixed an issue where accessing a SavedState ViewModel for the first time in Activity.onActivityResult() would result in an IllegalStateException . ( b/139093676 )
  • Fixed an IllegalStateException when using AbstractSavedStateViewModelFactory . ( b/141225984 )

ViewModel-SavedState Version 1.0.0-alpha05

18 سپتامبر 2019

androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-alpha05 is released. Version 1.0.0-alpha05 contains these commits .

API تغییر می کند

  • SavedStateViewModelFactory no longer extends AbstractSavedStateViewModelFactory and SavedStateHandle is created only for ViewModels that requested have it ( aosp/1113593 )

ViewModel-SavedState Version 1.0.0-alpha03

7 آگوست 2019

androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-alpha03 is released. The commits included in this version can be found here .

شکستن تغییرات

ViewModel-SavedState Version 1.0.0-alpha02

2 جولای 2019

androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-alpha02 is released. The commits included in this version can be found here .

ویژگی های جدید

  • Added SavedStateHandle.getLiveData() overload which accepts a default value.

API Changes

  • SavedStateVMFactory is renamed to SavedStateViewModelFactory .
  • AbstractSavedStateVMFactory is renamed to AbstractSavedStateViewModelFactory .

ViewModel-Savedstate Version 1.0.0-alpha01

13 مارس 2019

androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-alpha01 is released. The full commit log for this initial release can be found here .

ویژگی های جدید

  • Now ViewModels can contribute to savedstate. To do that you use newly introduced viewmodel's factory SavedStateVMFactory and your ViewModel should have a constructor that receives SavedStateHandle object as a parameter.

نسخه 2.1.0

Important changes since 2.0.0

  • Added LifecycleEventObserver for the cases when a stream of lifecycle events is needed. It is a public API instead of a hidden GenericLifecycleObserver class.
  • Added ktx extensions for LiveData.observe methods and Transformations.* methods.
  • Added Transformations.distinctUntilChanged , which creates a new LiveData object that does not emit a value until the source LiveData value has been changed.
  • Added coroutine support in ViewModels by adding the extension property ViewModel.viewModelScope .

نسخه 2.1.0

5 سپتامبر 2019

androidx.lifecycle:lifecycle-*:2.1.0 is released. The commits included in this version can be found here .

Version 2.1.0-rc01

2 جولای 2019

androidx.lifecycle:*:2.1.0-rc01 is released with no changes from androidx.lifecycle:*:2.1.0-beta01 . The commits included in this version can be found here .

Version 2.1.0-beta01

7 مه 2019

androidx.lifecycle:*:2.1.0-beta01 is released. The commits included in this version can be found here .

ویژگی های جدید

  • Lifecycles are graduated to beta: api introduced in previous alphas such as liveData extension functions for transformations and observations, ViewModel initialisation with property delegation and others are stabilised and not going to change.

Version 2.1.0-alpha04

3 آوریل 2019

androidx.lifecycle:*:2.1.0-alpha04 is released. The commits included in this version can be found here .

API تغییر می کند

  • Breaking change: the underlying API behind by viewModels() and by activityViewModels() has been changed to support a ViewModelStore directly, rather than only a ViewModelStoreOwner . ( aosp/932932 )

Version 2.1.0-alpha03

13 مارس 2019

androidx.lifecycle:*:2.1.0-alpha03 is released. The full list of commits included in this version can be found here .

API تغییر می کند

  • ViewModelProvider.KeyedFactory was removed. Second interface in addition to ViewModelProvider.Factory didn't compose well with new features as property delegation in Kotlin by viewmodels {} . ( aosp/914133 )

Version 2.1.0-alpha02

30 ژانویه 2019

androidx.lifecycle 2.1.0-alpha02 is released.

API تغییر می کند

  • LifecycleRegistry now contains a setCurrentState() method that replaces the now deprecated setState() method. ( aosp/880715 )

رفع اشکال

  • Fixed an issue where mock ViewModel instances would crash when the containing ViewModelStore was cleared. b/122273087

Version 2.1.0-alpha01

December 17, 2018

androidx.lifecycle 2.1.0-alpha01 is released.

ویژگی های جدید

  • Added LifecycleEventObserver for the cases when a stream of lifecycle events is needed. It is a public api instead of a hidden GenericLifecycleObserver class.
  • Added ktx extensions for LiveData.observe methods and Transformations.* methods.
  • Method Transformations.distinctUntilChanged was added. It creates a new LiveData object that does not emit a value until the source LiveData value has been changed.
  • Coroutine support in ViewModels: extension property ViewModel.viewModelScope was added.
  • Added ViewModelProvider.KeyedFactory , a factory for ViewModels that receives key and Class in create method.

نسخه 2.0.0

نسخه 2.0.0

21 سپتامبر 2018

Lifecycle 2.0.0 is released with one bugfix from 2.0.0-rc01 in ViewModel.

رفع اشکال

  • Fixed a ViewModel proguard rule that incorrectly removed constructors b/112230489

Version 2.0.0-beta01

2 جولای 2018

رفع اشکال

  • Fixed LifecycleObserver proguard rule to keep only implementations, not subinterfaces b/71389427
  • Fixed ViewModel proguard rules to allow obfuscation and shrinking

Pre-AndroidX Versions

For the pre-AndroidX versions of Lifecycle that follow, include these dependencies:

dependencies {
    def lifecycle_version = "1.1.1"

    // ViewModel and LiveData
    implementation "android.arch.lifecycle:extensions:$lifecycle_version"
    // alternatively - just ViewModel
    implementation "android.arch.lifecycle:viewmodel:$lifecycle_version" // For Kotlin use viewmodel-ktx
    // alternatively - just LiveData
    implementation "android.arch.lifecycle:livedata:$lifecycle_version"
    // alternatively - Lifecycles only (no ViewModel or LiveData).
    //     Support library depends on this lightweight import
    implementation "android.arch.lifecycle:runtime:$lifecycle_version"

    annotationProcessor "android.arch.lifecycle:compiler:$lifecycle_version" // For Kotlin use kapt instead of annotationProcessor
    // alternately - if using Java8, use the following instead of compiler
    implementation "android.arch.lifecycle:common-java8:$lifecycle_version"

    // optional - ReactiveStreams support for LiveData
    implementation "android.arch.lifecycle:reactivestreams:$lifecycle_version"

    // optional - Test helpers for LiveData
    testImplementation "android.arch.core:core-testing:$lifecycle_version"
}

نسخه 1.1.1

21 مارس 2018

Only one small change: android.arch.core.util.Function is moved from arch:runtime to arch:common . This allows it to be used without the runtime dependency, eg in paging:common below.

lifecycle:common is a dependency of lifecycle:runtime , so this change doesn't affect lifecycle:runtime directly, only modules that depend directly on lifecycle:common , as Paging does.

نسخه 1.1.0

22 ژانویه 2018

Packaging Changes

New, much smaller dependencies are now available:

  • android.arch.lifecycle:livedata:1.1.0
  • android.arch.lifecycle:viewmodel:1.1.0

API Changes

  • The deprecated LifecycleActivity and LifecycleFragment have now been removed - please use FragmentActivity , AppCompatActivity or support Fragment .
  • @NonNull annotations have been added to ViewModelProviders and ViewModelStores
  • ViewModelProviders constructor has been deprecated - please use its static methods directly
  • ViewModelProviders.DefaultFactory has been deprecated - please use ViewModelProvider.AndroidViewModelFactory
  • The static ViewModelProvider.AndroidViewModelFactory.getInstance(Application) method has been added to retrieve a static Factory suitable for creating ViewModel and AndroidViewModel instances.