Should I regret having refactored my app's every a...
# android
p
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
You can use requireContext() from Fragment, hopefully this helps you
p
That's what I'm doing right now
m
You shouldn’t pass context to your viewmodels, if that’s what you’re implying
r
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
I'm yet to use ViewModels but I will consider your advice when using it. I was just asking why fragments are bad?
t
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
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
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.