r/androiddev • u/Zhuinden • Nov 14 '20
Article Simplified Android development using Simple-Stack
https://medium.com/@Zhuinden/simplified-android-development-using-simple-stack-6e44ce808c353
u/twigboy Code Peeker, Air Waves & Diablo 2 Runewords Nov 14 '20 edited Dec 09 '23
In publishing and graphic design, Lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document or a typeface without relying on meaningful content. Lorem ipsum may be used as a placeholder before final copy is available. Wikipedia8n2hhd0si00000000000000000000000000000000000000000000000000000000000000
2
u/Zhuinden Nov 15 '20
I actually somewhat remember konmik/nucleus. It seems to do some tinkering to ensure that registered tasks are re-executed after process death, while allowing saved state persistence.
While scoped services are scoped to a top-level screen (or shared between top-level screens), you won't find something like what NucleusLayout is doing, but Fragment-level can be handled with implementing
Bundleablefor saved state persistence, andScopedServices.Registeredfor something to execute on creation of your service.2
u/twigboy Code Peeker, Air Waves & Diablo 2 Runewords Nov 15 '20 edited Dec 09 '23
In publishing and graphic design, Lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document or a typeface without relying on meaningful content. Lorem ipsum may be used as a placeholder before final copy is available. Wikipediaeohd67n1jso0000000000000000000000000000000000000000000000000000000000000
1
u/Zhuinden Nov 15 '20
I have a wiki and a bunch of samples, the tutorial one is the best (and the "extensions sample) imo.
I would love for the docs to be the best, but it's really hard to see it from the perspective of someone who hasn't written the code that drives it sometimes.
I don't think I've actually seen docs that describe what the lib doesn't do. Do you have some nice example I could compare with? That'd be really helpful.
I get how Android lifecycles and fragments work but I absolute detest the chore of managing it all. If simple-stack delivers with the promise of letting me concentrate on business logic then that's an absolute win.
I've definitely been getting that feedback βΊοΈ
2
u/twigboy Code Peeker, Air Waves & Diablo 2 Runewords Nov 15 '20 edited Dec 09 '23
In publishing and graphic design, Lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document or a typeface without relying on meaningful content. Lorem ipsum may be used as a placeholder before final copy is available. Wikipedia36mu10m1lig0000000000000000000000000000000000000000000000000000000000000
4
u/Zhuinden Nov 14 '20
This is our secret weapon to cutting through Android's nefarious lifecycles and navigation shenanigans. I've been hoping to write an article in the past.. well, actually 3.5 months about it, but I've only caught up with my plans now. π€
Anyways, hope you find it interesting! :)
3
1
u/muckwarrior Nov 15 '20
Could this handle multiple back stacks? For example if I wanted each item in a BottomNav to have an independent backstack.
2
u/Zhuinden Nov 15 '20
There is technically a sample where I've set up a global multistack, due to the ability to customize the "destination type" to describe what stack a key should belong to.
I had been considering built-in support of some kind, but then I had to implement a dynamic shared bottom nav with menu items dependent on authentication state at work, and was like "nope, nothing library-level is dynamic enough for that".
Hypothetically it is possible to use a Backstack in each Fragment to manage their child fragment stack (and not the Activity-level one), but I don't have a sample for that at this time.
1
u/Pzychotix Nov 18 '20
Curious how you handle the usual "no context references allowed" problem in your "ViewModel" example? I'm assuming Backstack eventually has some sort of reference back to the Activity/Context to deal with fragment navigations.
2
u/Zhuinden Nov 18 '20
backstack.setStateChanger(stateChanger)inonResume, andbackstack.removeStateChanger()inonPause. Handled automatically by the lifecycle integration (BackstackHostwhich the user never sees, but you add it withNavigator.install.)Operations are enqueued while there is no state changer, so they will get executed when focus comes back.
It's
onResume-based and notonStart-based because dialogs are cranky.1
u/Pzychotix Nov 18 '20
Gotcha, that makes sense then.
Weird thought, but would that handle multi-window cases, where you had your app in two separate activities showing at the same time? I'm imagining Activity A being in focus, but Activity B having some delayed navigation, thus doing a navigation intended for Activity B while Activity A was the state changer. Or are the backstacks per Activity?
1
u/Zhuinden Nov 19 '20
I've always used Backstack in Activity (and could generally get away with a single Activity for the whole app, due to not needing "multi-window multi-task" considerations), having 1 global backstack would take very special considerations that would involve a
BaseActivitywhich isn't something I'd force on others as a library (and I'm not going to define stuff in a global namespace as a static).The quirky answer to your question is "multi-window would not cause any issue with this since Android 10 and above due to multi-resume".
But technically anyone can customize the behavior by not using Navigator (which is the automatic Activity lifecycle integration entrypoint), and using Backstack directly instead.
0
Nov 28 '20
[deleted]
1
u/Zhuinden Nov 28 '20
Sure, although each time I had to implement bottom nav, it's always something slightly different in behavior, which is why I just can't make assumptions about it anymore.
My biggest surprise was no-Multi-stack, shared bottom nav, dynamic tabs based on authentication state.
What do you hope to see?
5
u/leggo_tech Nov 14 '20
I've always found simple-stack intriguing but the timing was never right to use it.
I've always thought that android navigation could and should be simpler. `blah.goToScreen(new ScreenB())` and `goBack()` make so much sense to me, and it's nice to hear that that's pretty much what single stack did.
A few other comments while reading:
"set out to make working with compound viewgroups as our core UI easy, but this article focuses on its usage with Fragments." Can I still just use ViewGroups? It seems like if I used this I would do so with the purpose of ditching fragments
Random: I've never published a library, but why do people publish to jitpack? A lot of other popular libraries don't make me add that line to jitpack. I think I'm curious because I one day want to publish a library and have no idea how. =)
"and handles state restoration, lifecycle events, and back presses" O_O
Haven't read those two other articles. will read (the second one seems really interesting)
"Scoped services, global services" It's amazing to me how long I really fought the concept of scopes, or maybe not fought them but instead how difficult it was (at least to me) to properly create these scopes in android. This is definitely something I enjoy with navScopedViewModels from nav aac.
Is it ready for compose? This was something that came to mind when I wrote out point #1, but it looks like you beat me to the punch. Nice to see you're thinking about this already. I could have sworn I read something from Zach or Zak (last name is either a K or a T) where they said that they really thought that aac nav was overly complex and over engineered to which ian lake said that you're not stuck with it. aac compose nav is made to handle like a billion use cases and so it will be naturally complex (at least that's what I got from it) so anyway... I do think that there is going to be some other compose navigation that is popular (I think there aare a few libaries that are already popular with it)
Nice article and nice work on the library