androidx.activity:activity-ktx
Official Description: Kotlin extensions for 'activity' artifact
CommonsWare’s Notes
The primary thing that we get from this package is the viewmodels()
extension function. Technically this is on ComponentActivity
from the
androidx.activity:activity
artifact, though in practice you will use it
mostly from FragmentActivity
, AppCompatActivity
, and similar classes.
viewModels()
is a property delegate, allowing you to use syntax like:
class MainActivity : AppCompatActivity() {
val viewmodel: MainViewModel by viewModels()
}
Then, when you first access viewmodel
, the delegate will look up the ViewModel
for you, so you can avoid the ViewModelProviders.of()
statement and a
lateinit var
, the way we used to get a ViewModel
.
However, be careful: just because this is now encapuslated in a property
delegate does not change the timing rules. Do not attempt to reference
your ViewModel
property until onCreate()
— in particular, do not try
to reference it from another property initializer.
Documentation
Versions
- Latest Stable: 1.2.0
- Latest Release Candidate: 1.2.0-rc01
- Latest Beta: 1.2.0-beta02
- Latest Alpha: 1.3.0-alpha02
- 1.3.0-alpha02
- 1.3.0-alpha01
- 1.2.0
- 1.2.0-rc01
- 1.2.0-beta02
- 1.2.0-beta01
- 1.2.0-alpha08
- 1.2.0-alpha07
- 1.2.0-alpha06
- 1.2.0-alpha05
- 1.2.0-alpha04
- 1.2.0-alpha03
- 1.2.0-alpha02
- 1.2.0-alpha01
- 1.1.0
- 1.1.0-rc03
- 1.1.0-rc02
- 1.1.0-rc01
- 1.1.0-beta01
- 1.1.0-alpha03
- 1.1.0-alpha02
- 1.1.0-alpha01
- 1.0.0
- 1.0.0-rc01
- 1.0.0-beta01
- 1.0.0-alpha08
- 1.0.0-alpha07
- 1.0.0-alpha06
- 1.0.0-alpha05
- 1.0.0-alpha04
- 1.0.0-alpha03
- 1.0.0-alpha02
- 1.0.0-alpha01