Extending Activity vs Application

from the CommonsWare Community archives

At January 28, 2020, 1:43pm, root-ansh asked:

I was recently working with firebase, and i needed a way to store and use a single firebase instance and its event listener across all other activities/fragments.

I am not much good with java , but i know this : when a class extends another class, the parent class’s constructor and the overriden functions would be called when child class constructor/ functions are called.

But what happens when an activity is referenced from a base class? how will their lifecycles be affected? what will happen if say firebase instance is initialised in Base Activity’s onResume but referred in child activity’s onStart()? What happens when multiple activity’s are referencing a base activity?Because When i open Activity B( extending parent activity X) from Activity A(extending parent activity Y), would they be calling the same activity X? I feel stupid and deeply confused. Also, how is the approach of using an Base Class different from using an Application class for storing firebase and other shared variables?

At January 28, 2020, 2:08pm, mmurphy replied:

My guess is that you should be using a singleton repository object that mediates communications between Firebase and other aspects of your app. While I do not demonstrate Firebase specifically, I show this sort of repository pattern in Elements of Android Jetpack and Exploring Android in the context of things like Room and Retrofit.

I do not know how you are using “referenced” here. It seems like you mean “extended”, referring to inheritance. If so, “referenced” is not a good choice of verb. For example:

class Book {
  List<Chapter> chapters;

class Chapter {
  Book book;

Here, we would say that an instance of Chapter references its Book, and an instance of Book references its chapters. Neither involve inheritance.

I am going to assume that “referenced” means “extended” in my answers that follow.

They are unaffected.

Nothing unusual will happen.

There is a single class named Activity X. There will be multiple instances (objects) of subclasses of Activity X. Given that, I do not know what you mean by “calling the same activity X”.

IMHO, they are unrelated.