The purpose of this app is to show the usage of the rememberScoped
function in the context of different lifecycle events.
The demo will instantiate different fake business logic
objects (FakeRepo
, FakeScopedViewModel
or FakeInjectedViewModel)
and either scope them with rememberScoped
, hiltViewModelScoped
or with the vanilla remember
from Compose, to illustrate the differences in memory
retention across different lifecycle events.
The remembered objects will be represented on the screen with their unique memory location by rendering:
- the object's toString representation in a
Text
Composable - a unique color for the object's instance using
objectToColorInt
as background - a semi-unique emoji for the object's instance (limited to list of emojis available in emojis)
The app contains the following screens:
- Main Activity. Purpose: show Fragment navigation/lifecycle events and Activity configuration changes (Activity recreation)
- Composable content. Purpose: show Activity configuration changes and entry point for ComposeActivity
- 2 Scoped objects
- Button to Navigate to ComposeActivity
- MainFragment. Purpose: show Fragment navigation/lifecycle events when MainFragment goes into the backstack, and then comes back and its View is
recreated
- 1 Not scoped object
- 2 Scoped objects
- Button to Navigate to FragmentTwo
- FragmentTwo. Purpose: push MainFragment into the backstack to destroy its View
- Composable content. Purpose: show Activity configuration changes and entry point for ComposeActivity
- ComposeActivity. Purpose: show Compose navigation/lifecycle events with Compose Navigation destinations
- first rememberScoped Compose destination. Purpose: show Compose navigation/lifecycle events when destination goes into the backstack, comes back and
its Composables are recreated
- 1 Not scoped object
- 2 Scoped objects
- Button to Navigate to first
- Button to Navigate to second
- Button to Navigate back
- second Hilt hiltViewModelScoped Compose destination. Purpose: show Activity configuration changes (Activity recreation) and Compose conditional UI (
in dark mode for this example) when using Hilt injected ViewModels
- 1 Not scoped object
- 1 Scoped object (FakeRepo with
rememberScoped
) - 1 Hilt Injected Scoped ViewModel (FakeInjectedViewModel with
hiltViewModelScoped
)
- first rememberScoped Compose destination. Purpose: show Compose navigation/lifecycle events when destination goes into the backstack, comes back and
its Composables are recreated
Here is a list of lifecycle events where rememberScoped
will retain objects:
- Android Configuration change. Examples: App size changes (like in split screen), light/dark mode switches, rotation, language, etc.
- Fragment goes into the backstack
- Composable destination goes into the backstack
Before | After backstack navigation & configuration change |
---|---|
![]() |
![]() |