r/android_devs • u/[deleted] • Jun 01 '20
Help BaseActivity vs. ActivityLifecycleCallbacks
Hi, devs!
So... I'm starting to make an architecture from scratch in order to learn how to do it (and having one done for personal projects). At my current job I started to understand how the arch is made, and my boss always explains to me the decissions he took to make it so.
But recently I read this article, so you can guess I never heard of ActivityLifecyclesCallbacks before (the same applies to fragments). I understand both of the approaches, but I want to ask you: what would you prefer, BaseActivity and BaseFragment of ActivityLifecycleCallbacks and FragmentLifecycleCallbacks?
I'd love to read some why, because, from my point of view, having a BaseActivity and then extending to another activity to add, for example, a menu drawer or fragments navigation, or whatever, seems to be a better approach, but again, I'm really new to this world and am trying to learn also from people with more experience.
Thank you all, cheers!
1
u/naked_moose Jun 01 '20
You can read about inheritance vs composition in general if you want to know more. Both have their drawbacks and benefits, but bad inheritance is always worse than bad composition. I would avoid Base classes whenever possible because you never know when someone else didn't avoid them, and you have to use it - some libraries still use BaseFragments that you have to inherit to get required functionality. Base classes for fragments and activities are also an easy way to get even more bloated fragments and activities, and they manage to be bloated on their own
1
Jun 02 '20
More bloatead? Could you explain that, please?
1
u/naked_moose Jun 02 '20
Activity and Fragment classes bear too much responsibility in Android already, to the point of being god classes. Any additional inheritance is just additional responsibility which probably shouldn't be there, and if possible extracted to separate classes
1
Jun 02 '20
Mmm, I see... But, with {Android,Fragments}LifecycleCallbacks, if you have to repeat code between Activities, how do you manage to not repeat that code?
1
u/Zhuinden EpicPandaForce @ SO Jun 02 '20
Honestly, you don't really need multiple Activities most of the time. See https://www.reddit.com/r/androiddev/comments/gc0yw3/a_reminder_that_single_activity_app_architecture/
1
Jun 03 '20
I don't mean to be rude, but I think a recommendation is different from an obligation. I couldn't need more than one activity or maybe I need more than two :)
It depends of the project, the architecture... There's a few factors, I think, to consider before using a multiple Activity approach or not :)
1
u/Zhuinden EpicPandaForce @ SO Jun 03 '20
Well once you're already stuck with multiple Activities, the simple solution has generally been
BaseActivity.Whether using an ActivityLifecycleCallbacks is "cleaner" is less of relevance than ease of understanding, imo. So when we had to share views between Activities, we had to include them in every Activity, and put the handler methods in the BaseActivity, even though we were aware of how to use compound viewgroups. The BaseActivity was unavoidable.
Well, unless we had done something like what https://github.com/frogermcs/DrawerLayoutInstaller does.
1
Jun 04 '20
If using ActivityLifecycleCallbacks, how do you manage to not repeat code between activities?
1
u/Zhuinden EpicPandaForce @ SO Jun 04 '20
Depends on what you're doing in the activity lifecycle callbacks.
1
Jun 04 '20
A generic case, for example, injecting a ViewModel or the Analytics manager, or something that would go in each activity you consider.
→ More replies (0)2
u/naked_moose Jun 02 '20
Don't create multiple Activities, and most of the time Fragment's code can be kept in a single onViewCreated callback. If it can't be kept there, you can also use viewLifecycleOwner.lifecycle to extract a generic version of fragment's view lifecycle to a separate class
1
u/CraZy_LegenD Jun 02 '20
Should i use base activity?
It depends
If you want to apply locale then one base activity will help you avoiding unnecessary copy paste, you need an injected model in every other activity perhaps a lifecycle bound utils that dispose resources on destroy event etc...
But if you're to have anything else that tries to abstract one behavior but you end up just adding more and more, it's a bad abstraction and shouldn't be done or just make an abstraction for that thing, for example abstractrecyclerview fragment, it should have an abstract adapter and layout manager and also live data to be observed, it's a great abstraction rather than abstractfragment and you add a LayoutManager that every other fragment inherited for no reason.
You should ask yourself: does this help me in the long run avoiding copy paste and is it readable?
1
Jun 03 '20
Yes, I think it does.
Where I'm working now we have this approach: a BaseActivity/BaseFragment and then we extend from there. The common code for every single one of them goes to the base. We don't add unnecessary code to the base if we don't really need it, the ViewModel is generated from a Factory, and everything else goes to each Activity.
About the redability point... I don't know, I think it depends of the person reading the code. You could understand it or not. I've been reading a lot of code from repositories with MVVM architectures and sometimes I understand what it's done and some times I don't...
11
u/Zhuinden EpicPandaForce @ SO Jun 01 '20
BaseActivity is an unfortunate byproduct of being able to add custom shared behaviors to Activities only through inheritance. For example, an RxLifecycleActivity needs to define BehaviorRelays, but they can only give you those through inheritance. Needless to say, using ActivityLifecycleCallbacks and a
Map<Activity, BehaviorRelay<ActivityEvent>>is significantly trickier than putting it in a BaseActivity, so I'd opt for inheritance in this case due to ease of use.However, sharing views between Activities is a painful thing. I'd only consider it if it's truly necessary - - for example, sharing a nav drawer between activities is much easier if, well, you have only 1 Activity and you don't even need to share it between Activities! Otherwise, you'd want to use a BaseActivity, and use
<includein each Activity layout, and that's pain.Needless to say, I've avoided the use of multiple Activities whenever possible, as single activity apps are generally much easier to reason about with a proper navigation framework (I use https://github.com/Zhuinden/simple-stack).
As for the original question, unless you're ref-counting activities, I think activity lifecycle callbacks are generally trickier approach.