evollu / react-native-firebase-analytics

React native bridge for firebase analytics
MIT License
206 stars 54 forks source link

[Android] Replaced 'getCurrentActivity' with 'getReactApplicationContext' #4

Closed xxsnakerxx closed 8 years ago

xxsnakerxx commented 8 years ago

Replaced getCurrentActivity() with getReactApplicationContext() for all methods in android module, because you can call analytics methods before the app will be initialized (getCurrentActivity returns null in this case and the app crashing)

xxsnakerxx commented 8 years ago

My usage example

analytics.js

import Analytics from 'react-native-firebase-analytics';

import { ENVIRONMENT, ENVIRONMENTS } from '../config';

// collect analytics only in production
if (ENVIRONMENT !== ENVIRONMENTS.PRODUCTION) {
  Analytics.setEnabled(false);
  Analytics.logEvent = (...args) => console.log('Analytics.logEvent', ...args);
  Analytics.setUserId = (...args) => console.log('Analytics.setUserId', ...args);
  Analytics.setUserProperty = (...args) => console.log('Analytics.setUserProperty', ...args);
}

export default Analytics;

In this case android app crashing on line Analytics.setEnabled(false); This PR fixed this.

evollu commented 8 years ago

i'm trying to understand the difference between getCurrentActivity() and getReactApplicationContext(). I'm using getCurrentActivity().getIntent() in another library but seems getReactApplicationContext() doesn't have it.

xxsnakerxx commented 8 years ago

As I understand Activity is an android view! FirebaseAnalytics.getInstance want to receive Context! But Activity just casting to AppContext...

https://developer.android.com/reference/android/content/Context.html https://developer.android.com/reference/android/app/Activity.html

evollu commented 8 years ago

looks like getReactApplicationContext() is the correct way to go in this case. thanks