Google is committed to advancing racial equity for Black communities. See how.

Activity

Access composable APIs built on top of Activity.
Latest Update Current Stable Release Next Release Candidate Beta Release Alpha Release
June 10, 2020 1.1.0 - - 1.2.0-alpha06

Declaring dependencies

To add a dependency on Activity, you must add the Google Maven repository to your project. Read Google's Maven repository for more information.

Add the dependencies for the artifacts you need in the build.gradle file for your app or module:

dependencies {
    def activity_version = "1.1.0"

    // Java language implementation
    implementation "androidx.activity:activity:$activity_version"
    // Kotlin
    implementation "androidx.activity:activity-ktx:$activity_version"
}

For more information about dependencies, see Add build dependencies.

Feedback

Your feedback helps make Jetpack better. Please let us know if you discover new issues or have ideas for improving this library. Please take a look at the existing issues in this library before you create a new one. You can add your vote to an existing issue by clicking the star button.

Create a new issue

See the Issue Tracker documentation for more information.

Version 1.2.0

Version 1.2.0-alpha06

June 10, 2020

androidx.activity:activity:1.2.0-alpha06 and androidx.activity:activity-ktx:1.2.0-alpha06 are released. Version 1.2.0-alpha06 contains these commits.

New Features

  • You can now destructure the ActivityResult class in kotlin to directly access the requestCode and intent. (b/157212935)
  • ActivityResultLauncher now allows you to get the ActivityResultContract that was used to register the launcher. (b/156875743)

API Changes

  • Breaking change: The invoke() method on ActivityResultRegistry has been renamed to onLaunch(). (b/157496491)
  • The OpenMultipleDocuments contract now returns an empty list instead of null to the registered callback if no result is returned. (b/157348014)

Version 1.2.0-alpha05

May 20, 2020

androidx.activity:activity:1.2.0-alpha05 and androidx.activity:activity-ktx:1.2.0-alpha05 are released. Version 1.2.0-alpha05 contains these commits.

New Features

API Changes

  • The TakePicture contract now returns a boolean indicating success rather than a thumbnail Bitmap as this was very rarely supported by camera apps when writing the image to the provided Uri. (b/154302879)
  • The invoke() extensions on ActivityResultLauncher have been removed in favor of explicitly using launch() to better indicate that these are asynchronous operations. Kotlin extensions for launch have been added to the androidx.activity.result package for ActivityResultLauncher<Void> and ActivityResultLauncher<Unit> that remove the need to pass in null or Unit, respectively, mirroring that behavior from the previously invoke() extensions. (aosp/1304674, aosp/1304675)
  • The IntentSenderRequest.Builder methods for setFlagsMask() and setFlagsValues() has been combined into a single setFlags() method. (aosp/1302111)

Bug Fixes

  • When registering an ActivityResultCallback with a LifecycleOwner, fixed an issue where the callback would be triggered before the state reaches STARTED. (aosp/1309744)

Behavior Changes

  • The ActivityResultRegistry now generates request codes starting at 0xFFFF rather than at 0, preventing overlap when using startActivityForResult() or requestPermissions() in an activity. (aosp/1302324)

Version 1.2.0-alpha04

April 29, 2020

androidx.activity:activity:1.2.0-alpha04 and androidx.activity:activity-ktx:1.2.0-alpha04 are released. Version 1.2.0-alpha04 contains these commits.

New Features

  • Added a contract that can call startIntentSenderForResult to the ActivityResult APIs. (b/153007517)

API Changes

  • The prepareCall() method has been renamed to registerForActivityResult(), both on ComponentActivity here and in Fragment 1.3.0-alpha04. (aosp/1278717)
  • The GetContents, OpenDocuments, and RequestPermissions contracts have been renamed to GetMultipleContents, OpenMultipleDocuments, and RequestMultiplePermissions, respectively. (aosp/1280161)
  • ComponentActivity now implements the ActivityResultRegisteryOwner interface. (aosp/1290888)
  • - The startActivityForResult()/onActivityResult() and onRequestPermissionsResult() APIs on ComponentActivity have been deprecated. Please use the Activity Result APIs. (b/154751887)

Bug Fixes

  • When using the GetMultipleContents and OpenMultipleDocuments contracts and selecting a single item, it is now correctly returned to your callback. (b/152941153)

Version 1.2.0-alpha03

April 1, 2020

androidx.activity:activity:1.2.0-alpha03 and androidx.activity:activity-ktx:1.2.0-alpha03 are released. Version 1.2.0-alpha03 contains these commits.

New Features

  • Added the TakeVideo, PickContact, GetContent, GetContents, OpenDocument, OpenDocuments, OpenDocumentTree, and CreateDocument contracts to the set of pre-built contracts provided by ActivityResultContracts. (aosp/1262482, aosp/1266916, aosp/1268960)
  • The Activity Result APIs now support launching an activity for result with an optional ActivityOptionsCompat. (b/151860054)

API Changes

  • The TakePicture contract now takes a Uri input for where the image should be stored. The previous contract that does not take any input has been renamed to TakePicturePreview. (aosp/1262482)
  • The registerActivityResultCallback() method on ActivityResultRegistry has been renamed to register(). (aosp/1267621)
  • The dispose() method on ActivityResultLauncher has been renamed to unregister() and the unregisterResultCallback() on ActivityResultRegistry has been removed. (aosp/1267621)
  • The createIntent() method of ActivityResultContact now takes a Context in addition to the input to make it possible to create explicit Intents. (aosp/1238800)
  • An ActivityResultContract can now override getSynchronousResult() to deliver a result without calling startActivityForResult. This is used by the RequestPermission and RequestPermissions contracts to correctly deliver a ‘granted’ status if the requested permissions are already granted. (b/151110799)
  • The previously available Dial contract has been removed as that Intent is not meant to be used with startActivityForResult(). (aosp/1266916)
  • Many of the Activity Result APIs not meant to be extended are now final. This includes getActivityResultRegistry(), the prepareCall() methods, all methods of ActivityResultRegistry except invoke(), and a number of the default contracts that do not support optional extras. (b/152439361)

Bug Fixes

  • Fixed a NullPointerException in ActivityResultRegistry when attempting to deliver results to a callback that has not yet been re-registered after a configuration change; ActivityResultRegistry now holds onto these pending results and delivers them when the callback is re-registered. (b/152137004)

Version 1.2.0-alpha02

March 18, 2020

androidx.activity:activity:1.2.0-alpha02 and androidx.activity:activity-ktx:1.2.0-alpha02 are released. Version 1.2.0-alpha02 contains these commits.

New Features

  • ActivityResultRegistry: ComponentActivity now provides an ActivityResultRegistry that lets you handle the startActivityForResult()+onActivityResult() as well as requestPermissions()+onRequestPermissionsResult() flows without overriding methods in your Activity or Fragment, brings increased type safety via ActivityResultContract, and provides hooks for testing these flows. See the updated Getting a Result from an Activity. (b/125158199)

Version 1.2.0-alpha01

March 4, 2020

androidx.activity:activity:1.2.0-alpha01 and androidx.activity:activity-ktx:1.2.0-alpha01 are released. Version 1.2.0-alpha01 contains these commits.

New Features

  • Added support for the ViewTreeLifecycleOwner.get(View) API added in Lifecycle 2.3.0-alpha01 such that it will return the Activity as the LifecycleOwner for any Views directly added to the Activity. (aosp/1182955)

Bug Fixes

  • Fixed a regression introduced in Activity 1.1.0 when running on older versions of the platform where onBackPressed() would cause an IllegalStateException due to a bug in the android.app.FragmentManager. (b/146290338)

Version 1.1.0

Version 1.1.0

January 22, 2020

androidx.activity:activity:1.1.0 is released. Version 1.1.0 contains these commits.

Important changes since 1.0.0

  • Lifecycle ViewModel SavedState Integration: SavedStateViewModelFactory is now the default factory used when using by viewModels(), the ViewModelProvider constructor, or ViewModelProviders.of() with a ComponentActivity or its subclasses.

Version 1.1.0-rc03

December 4, 2019

androidx.activity:activity:1.1.0-rc03 and androidx.activity:activity-ktx:1.1.0-rc03 are released. Version 1.1.0-rc03 contains these commits.

Dependency changes

  • Activity now depends on Lifecycle 2.2.0-rc03 and Lifecycle ViewModel SavedState 1.0.0-rc03.

Version 1.1.0-rc02

November 7, 2019

androidx.activity:activity:1.1.0-rc02 and androidx.activity:activity-ktx:1.1.0-rc02 are released. Version 1.1.0-rc02 contains these commits.

Dependency changes

  • Activity now depends on lifecycle 2.2.0-rc02.

Version 1.1.0-rc01

October 23, 2019

androidx.activity:activity:1.1.0-rc01 and androidx.activity:activity-ktx:1.1.0-rc01 are released with no changes from 1.1.0-beta01. Version 1.1.0-rc01 contains these commits.

Version 1.1.0-beta01

October 9, 2019

androidx.activity:activity:1.1.0-beta01 and androidx.activity:activity-ktx:1.1.0-beta01 are released. Version 1.1.0-beta01 contains these commits.

Dependency changes

  • Activity now depends on SavedState 1.0.0 stable.

Version 1.1.0-alpha03

September 5, 2019

androidx.activity:activity:1.1.0-alpha03 and androidx.activity:activity-ktx:1.1.0-alpha03 are released. The commits included in this version can be found here.

New features

  • Activity now depends on Core 1.1.0 stable.

Bug fixes

Version 1.1.0-alpha02

August 7, 2019

androidx.activity:activity:1.1.0-alpha02 and androidx.activity:activity-ktx:1.1.0-alpha02 are released. The commits included in this version can be found here.

New features

  • SavedStateViewModelFactory is now the default factory used when using by viewModels(), the ViewModelProvider constructor, or ViewModelProviders.of() with a ComponentActivity (b/135716331)

Version 1.1.0-alpha01

July 2, 2019

androidx.activity:activity:1.1.0-alpha01 and androidx.activity:activity-ktx:1.1.0-alpha01 are released. The commits included in this version can be found here.

New features

  • activity now depends on Lifecycle 2.2.0-alpha02. (aosp/1007817)
  • activity-ktx added a dependency on lifecycle-runtime-ktx; you no longer need to explicitly add it to your dependencies when using activity-ktx or libraries that depend on activity-ktx (such as fragment-ktx). (aosp/987162)

Version 1.0.0

Version 1.0.0

September 5, 2019

androidx.activity:activity:1.0.0 and androidx.activity:activity-ktx:1.0.0 are released. The commits included in this version can be found here.

Major Features of 1.0.0

  • ComponentActivity: ComponentActivity serves as the new base class for FragmentActivity in Fragment 1.1.0 and, by extension, AppCompatActivity in AppCompat 1.1.0.
  • activity-ktx: The activity-ktx module includes a by viewModels Kotlin property extension for accessing ViewModels. This module is automatically included when you include fragment-ktx from Fragment 1.1.0.
  • OnBackPressedDispatcher: As a composable alternative to overriding onBackPressed(), you can now register a OnBackPressedCallback from any LifecycleOwner (such as a fragment) to intercept system back button events. A lambda with receiver version addCallback has been added to activity-ktx. See Provide custom back navigation documentation for more details.
  • onRetainCustomNonConfigurationInstance deprecation: the onRetainCustomNonConfigurationInstance() and the related getLastCustomNonConfigurationInstance() APIs have been deprecated. It is strongly recommended to use ViewModels to store non-configuration state as they offer a composable solution suitable for any ViewModelStoreOwner that makes the ownership of the retained objects clear and provides an onCleared() callback for cleaning up resources when the activity is finally destroyed.

Version 1.0.0-rc01

July 2, 2019

androidx.activity:activity:1.0.0-rc01 and androidx.activity:activity-ktx:1.0.0-rc01 are released with no changes from 1.0.0-beta01. The commits included in this version can be found here.

Version 1.0.0-beta01

June 5, 2019

androidx.activity::activity:1.0.0-beta01 and androidx.activity:activity-ktx:1.0.0-beta01 are released with no changes from 1.0.0-alpha08. The commits included in this version can be found here.

Version 1.0.0-alpha08

May 7, 2019

androidx.activity:activity:1.0.0-alpha08 and androidx.activity:activity-ktx:1.0.0-alpha08 are released. The commits included in this version can be found here.

API changes

  • Breaking change: The previously deprecated addOnBackPressedCallback and removeOnBackPressedCallback methods on ComponentActivity have been removed. aosp/953857
  • The setEnabled() and isEnabled() methods of OnBackPressedCallback are now final. b/131416833
  • The remove() method of OnBackPressedCallback is now final. aosp/952720
  • OnBackPressedDispatcher now has public constructors, allowing you to construct your own instances for testing, etc. aosp/953431
  • onBackPressed() for ComponentActivity is now explicitly marked as @MainThread aosp/952721

Bug fixes

  • Fixed a ConcurrentModificationException when calling remove() from within the handleOnBackPressed() method of a OnBackPressedCalback that was added with a LifecycleOwner. b/131765095

Version 1.0.0-alpha07

April 25th, 2019

androidx.activity:activity:1.0.0-alpha07 and androidx.activity:activity-ktx:1.0.0-alpha07 are released. The commits included in this version can be found here.

API changes

This release makes significant changes to the handling of the onBackPressed(). See the updated custom back documentation for more details.

  • The methods for OnBackPressedCallback and OnBackPressedDispatcher have been marked as @MainThread. (aosp/943813)
  • The handleOnBackPressed() method no longer returns a boolean. Instead, OnBackPressedCallback is now an abstract class that can be enabled or disabled - only when the new isEnabled() method returns true will handleOnBackPressed() be called, in which you must handle the back button. (aosp/944518)
  • The addCallback methods of OnBackPressedDispatcher no longer return a Cancellable instance. OnBackPressedCallback now contain a remove() method that fulfill this functionality, allowing you to call remove() during handleOnBackPressed(). (aosp/944519) (aosp/946316)
  • activity-ktx now contains a receiver scoped callback for addCallback that accepts a lamdba that implements handleOnBackPressed() and has access to isEnabled and remove() (aosp/944520)

Version 1.0.0-alpha06

April 3rd, 2019

androidx.activity:activity:1.0.0-alpha06 and androidx.activity:activity-ktx:1.0.0-alpha06 are released. The commits included in this version can be found here.

API changes

  • ComponentActivity now contain a second constructor that takes a @LayoutRes int, which replaces the previous behavior of annotating your AppCompatActivity class with @ContentView. This approach works in both app and library modules. (b/128352521)
  • The OnBackPressedCallback related APIs on ComponentActivity have been deprecated in favor of the new OnBackPressedDispatcher, retrievable via getOnBackPressedDispatcher(). (aosp/922523)
  • Methods to add a new OnBackPressedCallback to the OnBackPressedDispatcher now return a Cancellable object, allowing removal of the callback without requiring an explicit reference to the OnBackPressedDispatcher. (aosp/922523)
  • Adding a OnBackPressedCallback with an associated LifecycleOwner now results in adding and removing the OnBackPressedCallback as the Lifecycle is started and stopped, respectively. (aosp/922523)

Version 1.0.0-alpha05

March 13th, 2019

androidx.activity:activity:1.0.0-alpha05 and androidx.activity:activity-ktx:1.0.0-alpha05 are released. The full list of commits included in this release can be found here.

New features

  • @ContentView annotation lookups are now cached (b/123709449)

Version 1.0.0-alpha04

January 30, 2019

androidx.activity:activity 1.0.0-alpha04and androidx.activity:activity-ktx 1.0.0-alpha04 are released.

New features

  • Added support for the @ContentView class annotation that allows you to indicate which layout XML file should be inflated as an alternative to using setContentView(). (aosp/837619)

API changes

  • Added a note that getViewModelStore() should not be overridden and will be made final in a future release. Please file a feature request if you are currently overriding this method. (aosp/837619)

Bug fixes

  • The activity module now depends on version 2.1.0-alpha02 of ViewModel to match the activity-ktx module’s dependency.

Version 1.0.0-alpha03

December 17, 2018

androidx.activity 1.0.0-alpha03 is released.

New features

  • ComponentActivity now implements BundleSavedStateRegistryOwner and depends on the newly released SavedState library [aosp/815133]
  • ComponentActivity now works around an Android framework bug that would cause InputMethodManager to leak the last focused view [b/37122102]

Version 1.0.0-alpha02

December 3, 2018

API changes

  • Added a note that getLifecycle() should not be overridden and will be made final in a future release. Please file a feature request if you are currently overriding this method. (aosp/815834)

Version 1.0.0-alpha01

November 5, 2018

androidx.activity 1.0.0-alpha01 introduces ComponentActivity, a new base class of the existing FragmentActivity and AppCompatActivity.

New features

  • You can now register an OnBackPressedCallback via addOnBackPressedCallback to receive onBackPressed() callbacks without needing to override the method in your activity.
  • Added a new by viewModels() Kotlin property delegate for retrieving ViewModels from a ComponentActivity.
  • Pending input events (such as clicks) are now canceled in onStop().

API changes

  • The implementation of LifecycleOwner and ViewModelStoreOwner have been moved from FragmentActivity to ComponentActivity.
  • onRetainCustomNonConfigurationInstance has been deprecated. Use a ViewModel for storing objects that need to survive configuration changes.