Add Activity non-config instance map-like API
This re-deprecates the single instance non-config instance API. Previously this was deprecated in favor of retained fragments. It was then undeprecated and platform fragments were deprecated (although those two events were not related). The problem here is that there needs to be a composable mechanism of retaining non-config instances for libraries to use. This is evidenced in Activity.java itself which already emulates a map internal by using a type and named fields. The Jetpack library (nee support library) has historically needed this as well, and has been forced to resort to marking the single non-config instance API as final, exposing a differently-named overload, and then coalescing everything into a single type with named fields, just like Activity.java. This change adds a class-keyed map-like API so that anyone can hook in without being directly in the activity subtype hierarchy. Unlike the single non-config instance API, the map-like API has a few different behaviors: - You can store an instance into the map anytime between onCreate and onDestroy (including inside those two lifecycle methods). The old API had a dedicated callback. You can use isChangingConfiguration() in onDestroy to conditionally create the retained instance. - Stored instances are available for the entire lifetime of the activity instance. The old API cleared the non-config object in onResume. - By virtue of the previous behavior, instances are retained across multiple config changes without having to re-create the state. While behaviorally different from the old API, this is more in line with the behavior of retained fragments or non-framework solutions like Jetpack fragments and view models. A compatibility shim for this in Jetpack will be added. Bug: 152476567 Test: atest ActivityNonConfigInstanceTest Change-Id: Ib2484d43327dd52e9cceac1f7201a5b4f694f7bb
Loading
Please register or sign in to comment