https://kotlinlang.org logo
Title
p

pavi2410

06/07/2019, 5:39 PM
Should I regret having refactored my app's every activity into fragments to use navigation architecture component? I read this: https://kotlinlang.slack.com/archives/C0B8M7BUY/p1551471716045900?thread_ts=1551471716.045900&cid=C0B8M7BUY
The painful thing I felt was getting
context
.
a

António Bastião

06/07/2019, 5:52 PM
You can use requireContext() from Fragment, hopefully this helps you
p

pavi2410

06/07/2019, 5:53 PM
That's what I'm doing right now
m

matt tighe

06/07/2019, 9:06 PM
You shouldn’t pass context to your viewmodels, if that’s what you’re implying
r

rkeazor

06/08/2019, 2:19 AM
Lol, there is nothing wrong with it. Nav Component is good, in my opinion.
And context doesnt belong in viewmodel. An Activity context in viewmodel would cause a memory leak. You can pass a ApplicationViewModel, and use AndroidViewModel
p

pavi2410

06/08/2019, 3:07 AM
I'm yet to use ViewModels but I will consider your advice when using it. I was just asking why fragments are bad?
t

tseisel

06/08/2019, 9:23 AM
There was a time where fragments were rather hard to use : not really in sync with the activity lifecycle and introduce a lot of complexity (new lifecycles methods, random state loss crashes, asynchronous fragment transactions...). That's why some Android developers learned the lesson and started to advocate against using them, preferring custom Views. Today, fragments have been improved a lot, and IMHO are better than having to maintain a bunch of activities. Not 2 projects have the same architecture. Use fragments, ViewModels or Navigation Component if those make sense for you project. There is no "best solution", try them and see if it makes your life easier. That's why software engineering is a creative field 😉
a

António Bastião

06/08/2019, 11:58 AM
I don’t get from where ViewModels come, but I thought the initial topic was about Fragments and navigation libraries. 😅 Well, add context into viewModels my experience says that we’ll be warned about memory leaks and avoid this behavior. Also I don’t see why context could be needed on a view model. Even if we need some resources we can pass the res int id to Fragments and get string(int), color(int) from there. 🤔
h

Hadi Tok

06/09/2019, 9:03 PM
You will find different opinions online. If you can have a judgement, you should use your judgement. If you can’t you should try if it would work for you. Your expectations might be different from others.