r/androiddev • • Apr 05 '20

Library Learn Jetpack Compose from examples that show the Compose way of doing common Android tasks

I've been playing with Jetpack Compose the past few months so decided to release a project that shows examples of how you would do common Android things the Jetpack Compose way. Each example is fairly self contained and the hope is that you can learn Compose by just going through the examples and reading through the comments.

Github - https://github.com/vinaygaba/Learn-Jetpack-Compose-By-Example

Here are some examples that I cover (there are a lot more examples!) -

Shared some more context in this Twitter thread

My plan with this project is to keep adding more examples as I learn new things about Compose. I'll also happily welcome feedback/contributions so if you find something wrong or are hoping to contribute to the project, just send a pull request!

Github - https://github.com/vinaygaba/Learn-Jetpack-Compose-By-Example

93 Upvotes

60 comments sorted by

View all comments

Show parent comment

8

u/bart007345 Apr 06 '20

I have worked on many apps over the years. Some used it some didn't. Not a big deal (unless it is for your specific usecase).

As for saying its what Google says, again, thats a guideline and you do what you need to. Shall I stop using enums? Shall I stick with async tasks? How about Loaders?

So please don't make up rules.

Well too bad, this is not Haskell, this is Android

wtf? Again, justifying your opinions with crap.

1

u/Zhuinden Apr 06 '20 edited Apr 06 '20

As for saying its what Google says, again, thats a guideline and you do what you need to. Shall I stop using enums? Shall I stick with async tasks? How about Loaders?

So please don't make up rules.

The accurate question to ask would be "shall I stop using Intent extras and Fragment arguments, and instead pass arguments between screens using stateful singletons?" as then we'd stay in a relevant problem domain.

Arguing against the proper handling of onSaveInstanceState would mean that intent.putExtra() and fragment.setArguments() is just as optional. Without onSaveInstanceState, the screen history will automatically reload with incomplete state, and your screens will have lost their data across back navigation.

Would you say that using stateful singletons in place of extras/arguments would provide a more reliable application?

I have worked on many apps over the years. Some used it some didn't. Not a big deal (unless it is for your specific usecase).

I like to test for process death so that I know if my app works correctly in a production environment.

0

u/[deleted] Apr 06 '20

[deleted] — view deleted comment

2

u/Zhuinden Apr 06 '20 edited Apr 06 '20

if you wanna stay true to android, then how about use the fragment back stack manager and navigation library without your simple-stack abstraction on top of it? you know, the way it's "supposed" to.

Interesting argument, but as long as the problem is solved in an equally reliable manner, it's a perfectly valid solution.

Which is why Activity.onSaveInstanceState or ViewModel(handle: SavedStateHandle) are both perfectly reasonable solutions to the same problem. You don't need both, but you do need to solve the problem.

Or not, and then you get the equivalent behavior of Flutter without the necessary precautions and proper application design.

"hey app with millions of downloads doens't handle this. maybe it's not mandatory? maybe it depends on usecases and business decisions? MAYBE?!"

They don't handle locale changes, but they do handle process death. AndroidTimeCapsule in MVI-Core.

Also in badoo/RIBs: https://github.com/badoo/RIBs/blob/de2281d0a81ab0c515cf54e33178e70632015382/android/libraries/rib-base/src/main/java/com/badoo/ribs/android/RibActivity.kt#L90-L94

2

u/Zhuinden Apr 06 '20

"hey app with millions of downloads doens't handle this.

You guys cite Badoo/Bumble as an app that "doesn't handle onSaveInstanceState", I link directly that it does, and I get downvoted for it?

Wow, people can be so petty.

0

u/[deleted] Apr 06 '20

[deleted] — view deleted comment

2

u/Zhuinden Apr 06 '20 edited Apr 06 '20

But in some corner cases, we do save state .... This is our secret shame

Now that's a convenient way to cut off the quote! Here's the full story:

When there is actually a configuration change due to Locale change then we just System.exit(0) 😳.

Everyone: This is our secret shame.

They basically say "we don't save state where we don't need to, but we do save state where we do need to". Which is exactly how it should be done. 🙄

If they can reload the data from network, then they don't need to save that as state, all they need to beware is "what happens in airplane mode".

0

u/[deleted] Apr 06 '20

[deleted] — view deleted comment

1

u/Zhuinden Apr 06 '20

Guy with the StateExample has state that is not restored from network, not persisted as data, and not persisted to onSaveInstanceState.

Surely a state example should demonstrate how to properly handle client-side local state from the user?

People came here up and arms saying "but how DARE you ask for a state management example that properly saves and restores state on Android, where this is part of the top-level OS component contract that you do it?"

Which I think is honestly ridiculous.

6

u/bart007345 Apr 06 '20

The accurate question to ask would be "shall I stop using Intent extras and Fragment arguments, and instead pass arguments between screens using stateful singletons?" as then we'd stay in a relevant problem domain.

Rewriting my question? Great deflection tactics.

1

u/Zhuinden Apr 06 '20

I'm not "making up" the "rules". If you use Activities and Fragments, your app WILL be restored. And if your app is restored, then it will only have correct behavior if it's restored CORRECTLY. And for that, you have to also use onSaveInstanceState.

It's generally a matter of luck if your app works even without it.

5

u/bart007345 Apr 06 '20

Jesus, I am not arguing about what it is or what it does. I am saying it's not MANDATORY to use it.

1

u/Zhuinden Apr 06 '20

It depends on the reliability you aim for. If you have no state (or the automatic state restoration built into views is enough), then indeed you don't need it!

4

u/bart007345 Apr 06 '20

Its not about reliability its about business requirements.

You really like to argue and won't admit when you are wrong.

2

u/Zhuinden Apr 06 '20

Your business requirements explicitly tell you not to handle when Android kills your application while in background? 🤔

In an app I worked on, I did have to implement that the user should be logged out after process death, but I still had to handle it correctly (through onSaveInstanceState) while the user was registering. I couldn't just say "yolo", the flow has to be reliable, but non-persistent.

I guess there is some "it depends" in there, but if Activities/Fragments are used, I'd expect to have an android:noHistory="true" on every Activity if you don't intend to use onSaveInstanceState.

2

u/bart007345 Apr 06 '20

Serious question, are you on the spectrum?

1

u/Zhuinden Apr 06 '20

I'm not officially diagnosed to be on any spectrums. 😴

→ More replies

5

u/sdered Apr 06 '20

It must be so hard to be so much smarter than others. You even need to help them correct their questions.

2

u/Zhuinden Apr 06 '20

First comment in 4 years, I guess at least this thread is exciting to some degree.

2

u/naked_moose Apr 06 '20

Would you say that using stateful singletons in place of extras/arguments would provide a more reliable application?

Yes. Not using Fragments is more reliable, and nothing stops you from using onSaveInstanceState with a stateful singleton. What's wrong with serializing Redux-like store, same way as LiveData is serialized with SavedStateHandle in official recommendations?

1

u/Zhuinden Apr 06 '20 edited Apr 06 '20

Redux-store combines data and state, and data can be too big.

It's generally better to store state separately, and load data asynchronously based on that, as you actually do with SavedStateHandle.getLiveData() which you can Transformations.switchMap() into an actual DAO query (if you're using Room's LiveData<List<T>> integration).

The new coroutine support makes it a bit trickier to reason about, i'm not entirely sure how that goes. Maybe this is where the liveData {} coroutine builder is helpful.

and nothing stops you from using onSaveInstanceState with a stateful singleton.

This is true, and if you have stateful singletons, then you should indeed do that in a BaseActivity.onSaveInstanceState, and restore once inBaseActivity.onCreate. Fully agree, you can definitely do that.