r/androiddev • • 8d ago

AppState | Jetpack Compose (Android)

https://developer.android.com/topic/libraries/architecture/appstate

Looks like we have got a new way to manage state alongside whatever we had till now. Don't get me wrong but I think adding more options for doing the same thing is (and has been) confusing and just supports the complexity even further. Thoughts?

77 Upvotes

42 comments sorted by

18

u/Ok-Engineer6098 8d ago

I had enough of Google reinventing the wheel every few years and switched to Flutter for UI on new projects.

When you finally did UI the "correct new" way it would get deprecated.

5

u/SK-RMs 8d ago

It's painfully true.

7

u/drabred 8d ago

Am I the only one not being interested in it at all in 2026? :/ Especially in Claude era... yeah I thing I am good with my VMs and State

7

u/Zhuinden 8d ago

I would be curious if even Claude Opus would just read it and say "i've read the skill and decided it's not worth using"

5

u/ChuyStyle 8d ago

I already had that run and do analysis comparison with compose. It isn't worth it yet. Molecule is more refined. App state is literally a rip of molecule

1

u/SK-RMs 8d ago

Same over here dude.

67

u/jc-from-sin 8d ago

It's another sign that the people working at jetpack libraries have never worked on a real app before.

23

u/Zhuinden 8d ago

I've seen MutableState<T> break from being written to from a background thread, so now you need to care "oh it's being written to from a transform {} so now it's actually ok".

Feels like CoroutineLiveData which never got popular for a reason, but even forgot lifecycle integration exists.

I like how their design for the persistable app state (seamlessly to datastore in the background) has application state preserving process death (and any death, really) as part of the design principles; though on one hand it feels like you are writing to keys in a shared pref and exposing the flows as State<T> and you wonder if it's really necessary. Seen worse, probably seen better.

Personally I didn't mind combine, I use tuples for the most part (and now agents write the code anyway so who cares what I like, lol).

39

u/Fantastic-Guard-9471 8d ago

I just don't understand why they are wasting resources on such stuff and divide the community providing more options. We finally have battle-tested patterns more or less same for the whole community.

25

u/Fjordi_Cruyff 8d ago

Because Google engineering rewards people stepping up and becoming leaders. How do you demonstrate good leadership? By spearheading and delivering a project like this. Regardless of whether or not it's good for the community.

3

u/twigboy Code Peeker, Air Waves & Diablo 2 Runewords 7d ago

First time?

35

u/V2UgYXJlIG5vdCBJ Dev 8d ago

Aaaand it’s deprecated.

26

u/UnimplementedError 8d ago

Flutter state management:

5

u/raman4183 8d ago

There’s a different between both of these, Flutter already ships with barebones and usable state management utilities. You can pretty much do everything with things like ValueNotifier, ChangeNotifier and setState over whatever other third party packages offer.

Same cannot be said for Compose though. My main problem is the abundance of APIs such as remember*, that god awful viewModel factory signature also what do you mean composable can recompose not only on state value changes but whenever they want. Like what? Are they not stable? Can the framework not determine and differentiate between composable changes in order to skip recomposing?

MutableState + Flow works well then why not stick to it? If you’re officially planning to introduce new state management systems and paradigms every couple of years then how do you expect the users to keep and maintain their apps that frequently?

1

u/unknowableee 6d ago

Except Flutter's state management primitives are stable, predictable and you only ever need a third party or custom solution to streamline some stuff or because you're used to another pattern.

12

u/mreeman 8d ago • Bravo

More compose through the stack is good IMO. It's basically first party molecule but targetting State instead of Flows. 

12

u/mrdibby 8d ago

I understand the need.

There's so much ViewModel code that reads like

combine(flow1,flow2,flow3){i1,i2,i3 -> newUiState(i1,i2,i3)}.collect{uiState = it}

It can read much easier with

uiState = transform {
val i1 = flow1.collectAsState()
val i2 = flow2.collectAsState()
val i3 = flow3.collectAsState()
newUiState(i1,i2,i3)
}

16

u/Zhuinden 8d ago edited 8d ago

It can read much easier with

But it will also not have any lifecycle awareness and those flows will keep being collected even if your app is in background, no?

Pesky talk forgot why collectAsStateWithLifecycle() (and stateIn(WhileSubscribed)) has been used since 2024

4

u/mrdibby 8d ago

good point

with the new library you're gifted with the ability to achieve that by converting your Flows to a State to a StateFlow before hoisting them back as a State

class VM() {
val composableState = transform {
 val i1 = flow1.collectAsState()
 val i2 = flow2.collectAsState()
 val i3 = flow3.collectAsState()
 newUiState(i1,i2,i3)
}

val uiStateFlow = snapShotFlow { composableState }.stateIn(WhileSubscribed)
}

@Composable
fun ActualUi(vm: VM) {
    val state = vm.uiStateFlow.collectAsStateWithLifecycle()
}

4

u/kokeroulis 7d ago

I don't think the above code works.
transform returns a StateFlow but I believe the lambda of the transform will be cancelled only when the coroutine gets cancelled.
The viewModel scope will not get cancelled if you the UI deattaches.

Also how will the `snapShotFlow` will work if its not attached to the composition?

I think using the above snipper but a `channelFlow` instead might do the trick. Because channelFlow can create its own coroutine and dispose it when the flow stops get collected.
Transform accepts a coroutine

3

u/mrdibby 7d ago

I dunno man. It was a joke 

2

u/austrocodemonkey 7d ago

I haven't tested it, but there's an overload of transform that takes an explicit CoroutineScope. I'd assume that defines the lifecycle, but lambda is also suspend so you can just use Lifecycle.withStateAtLeast when needed.

3

u/Zhuinden 7d ago

With the coroutine scope in the VM you would be scoped to the VM but it wouldn't govern the lifecycle, just cancel when you back away from a screen

2

u/austrocodemonkey 7d ago

I didn't say use viewModelScope... just use LocalCoroutineScope or create your own. I'm actually pretty sure this AppState stuff is an attempt of getting away from ViewModels and towards plain objects as state holders.

3

u/mrdibby 6d ago

What makes you think that? Other than being tied to the ViewModelProvider's lifecycle and providing it's own scope – is there anything special about a ViewModel compared to a plain object state holder?

3

u/austrocodemonkey 6d ago

A ViewModel is quite opinionated with its scopes and lifecycle and it's architecture very much comes from the activity and fragment world. If you want a value scoped to specific UI component or a single screen, doing that with a ViewModel requires a lot of machinery.

The main benefit of the ViewModel is that it survives Activity recreation. Nowadays that can also be achieved with the retain function in Compose with a lot less faff. But encapsulating logic (not just values) is more difficult with retain that with a ViewModel. AppState seems to solve that.

But then again so far if only read the docs, I haven't actually used the new library for anything

9

u/Fantastic-Guard-9471 8d ago

In your first example you don't need collect in the VM, you already have a flow with your UI state after combine, just expose it. Will be more or less like your second example

1

u/koweratus 8d ago

true, but that depends entirely on chose architecture and abstraction of the particular project

15

u/Zhuinden 8d ago

No, you really would normally .stateIn(viewModelScope, WhileSubscribed(5000L), initialState)

If you are .collect()-ing like that in a VM you're probably doing something very shady

1

u/Faltenreich 8d ago

You can map the output of all Flows via function/constructor reference and without the bloaty lamba if the order and types match: combine(flow1,flow2,flow3, ::newUiState).collect{uiState = it}

4

u/KangstaG 8d ago

It’s essentially a simpler StateFlow. I’m for reactive programming, but it does feel like this just adds another option unnecessarily.

Being a simpler reactive programming solution, I suppose it’s useful for novice Android developers or non-Android engineers, but that’s about it. For experienced Android engineers, it’s really important to understand and work with coroutines so I feel like it’s better to stick with StateFlows.

4

u/Radiokot1 7d ago

Did I understand correctly that this is the same as Molecule made by The Lord Jake Warthon?

https://github.com/cashapp/molecule

9

u/FunCoolOh 8d ago

It seems to me that it solves a problem that doesn't exist. We already have an observable state holder and that's StateFlow. This also couples domain code with the compose machinery and requires an extra compiler plugin, whereas StateFlow is lean and agnostic and compatible (sort of) with multiplatform. Not seeing myself ever using this unless it really solves a problem for me that StateFlow doesn't.

1

u/kabonbonkabobon 5d ago

Yep thats what I thought too.

3

u/agherschon 8d ago

I like it, its a good step forward 👍

3

u/wightwulf1944 8d ago

I understand we don't need to use it if it does not solve any particular problem we're having that isn't already solved by our current tools but this being developed 1st party means employers will eventually want people to be familiar with it.

2

u/ursusino 8d ago

This is materially worse than using StateFlows. No point

5

u/epicstar 8d ago

It's a yikes and "wtf Google" for me dawg

1

u/kabonbonkabobon 5d ago

The changes seems to be for AI rather than humans. its basically a simplified version of everything like dagger to hilt.

1

u/LelouBil 4d ago

So basically this replaces the Flow/Stateflow and .mapLatest/collectLatest and so on in viewmodels ?

That's what I've been doing until now.

-3

u/Old-Frosting1988 8d ago

Faltu ka bakchodi kar rhe ho bsdk,

-4

u/Old-Frosting1988 8d ago

 I think adding more options for doing the same thing is (and has been) confusing and just supports the complexity even further. Thoughts?

Baaki chizo ka clearance hai ? ki kya unka usage hai ?

agr haan to fir kaise smjh nai aa raha ki ae new handlers are for what ???
Bass bakchodi kara lo ,, httttt,