2

We know that, we can log event in an activity using the code:

FirebaseAnalytics.getInstance(this).logEvent("hello", new Bundle());

Suppose I have this class "Student", which is a simple class called inside different activities. All I want to know is how to use FirebaseAnalytics inside this "Student" class without passing the context or getApplicationContext() or "this" keyword?

N.B: FirebaseCrashlytics can be used in "Student" class as follow:

FirebaseCrashlytics.getInstance().log("logstudentclass");

Also "Student" class be like:

public class Student{
     private String name;
     private String roll;
      
     public Student(String name, String, roll){
         this.name=name;
         this.roll=roll;
     }
     // bla bla bla methods
     // bla bla bla getters setters
}
  • Hi, check https://stackoverflow.com/a/5114361/2649154 to see how you can get application context outside of Activity/Fragment. – gioravered Aug 29 '21 at 08:38
  • In that case, I need to declare a function to get the Application Context for every Activity class. Is there any other better way? – Md. Mahir Labib Aug 29 '21 at 09:20

2 Answers2

0

You can create a class, extending Application class:

Create a file MyApplication.java with the following code:

class MyApplication extends Application {
    private static Application context;

    public void onCreate() {
        super.onCreate();
        context = this;
    }

    public static Context getAppContext() {
        return context;
    }
}

This class extends the Application class and lives throughout the entire lifetime of the application. Using a static variable for storing the Application value (your Context), and a static method to get it, makes it approachable from anywhere in your code.

in your AndroidManifest.xml - state that you want to use MyApplication class as your application class:

<application
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:name=".MyApplication">

    ...
</application>

Wherever you want to get the application Context, use:

MyApplication.getAppContext()

In Your case:

FirebaseAnalytics
    .getInstance(MyApplication.getAppContext())
    .logEvent("hello", new Bundle());

More about Application class here.

Note that there are downsides for this approach, for instance, trying to use the context before it was initialized, string translations, and more so use it carefully.

gioravered
  • 1,758
  • 3
  • 19
  • 30
  • Suppose, I don't know in which Activity my class is used. In that case, what should I do to get the Application context? Should I build a common getContext() function in every Activity class? – Md. Mahir Labib Aug 30 '21 at 16:00
  • You use MyApplication.getAppContext() from where you want. – gioravered Aug 30 '21 at 16:32
0

You can instantiate a public static reference to FirebaseAnalytics in your Application class

   public class MyApp extends Application {
    public static  FirebaseAnalytics firebaseAnalytics;
    @Override
        public void onCreate() {
            super.onCreate();
            firebaseAnalytics = FirebaseAnalytics.getInstance(this);
    }
   }

Next, you add the same name to your Android Manifest file, in the Application tag:

<application
  android:name=".MyApp"
  ...

Now you can use it inside Student like:

MyApp.firebaseAnalytics.log("logsstudentclass")
vivek.kartha
  • 308
  • 1
  • 3
  • 14