r/android_devs • • 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!

12 Upvotes

20 comments sorted by

View all comments

Show parent comments

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

u/[deleted] 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

u/[deleted] 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

u/[deleted] 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)