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

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 <include in 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.

1

u/[deleted] Jun 02 '20

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 <include in each Activity layout, and that's pain.

But I don't want to share views, the menu was an example of having a BaseActivity and then a BaseMenuActivity (which extends from BaseActivity), and just having an activity with a menu.

As for the original question, unless you're ref-counting activities, I think activity lifecycle callbacks are generally trickier approach.

I don't know what you mean by ref-counting, sorry, could you explain it, please?

Thank you for your reply!

1

u/Zhuinden EpicPandaForce @ SO Jun 02 '20

and then a BaseMenuActivity (which extends from BaseActivity), and just having an activity with a menu.

I would not do that, and advise watching https://youtu.be/29MAL8pJImQ?t=1372.

I don't know what you mean by ref-counting, sorry, could you explain it, please?

For example, tracking if there are any activities that are resumeed (or started) to figure out if your application is in front.

Of course, ProcessLifecycleOwner does something very similar, too.

A while ago I also had to "keep main-thread Realm instance alive only if there is at least 1 created activity", that is also ref-counting.

I've seen ActivityLifecycleCallbacks used in conjunction with Dagger-Android before, that is smart but I never really relied on it as I didn't need that trick. It's in the GithubBrowserSample.