-2

In my app i need to store the location to firebase so im using two location classes as application because i need to store location for two different entities

First Application class

public class FirstApplicationClass  extends MultiDexApplication implements  GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    LocationListener{ 
}

Second Application Class

public class SecondApplicationClass extends FirstApplicationClass implements GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    LocationListener {
}

Below is manifest.xml code

<application
    android:name=".location.SecondApplicationClass"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"

i need to use this.getApplicationContext(); in both the case, in the second case where i am using the Second application class, i am able to use this.getApplicationContext(); from the specific activity i.e,

mSecondApplicationClass = (SecondApplicationClass) this.getApplicationContext();

and able to store the location. I dont know how to use this.getApplicationContext(); with the First Application Class, i.e,

mFirstApplicationClass = (FirstApplicationClass) this.getApplicationContext();

i am unable to achieve this

MIDHUN CEASAR
  • 131
  • 2
  • 9
  • `Second Application Class` ? i dont see any application class extension in `SecondApplicationClass` – Santanu Sur Apr 12 '18 at 11:50
  • Your second 'Application class' is not an application class. If it was, it would have derived from `Application` or (as your other application class) `MultiDexApplication`. The second is deriving from a `LocationService` (which is probably a service and therefore a context) that's why you can call `getApplicationContext()`. You don't need to call that in `FirstApplicationClass` because that instance is returned when you call `getApplicationContext()`. – 0xDEADC0DE Apr 12 '18 at 11:52
  • Sorry that was a typing mistke – MIDHUN CEASAR Apr 12 '18 at 12:01

1 Answers1

0

In an android app, ApplicationContext is unique

There is no difference between ApplicationContext in your FirstApplicationClass or SecontApplicationClass.

in AndroidManifest.xml file you are introducing the SecondApplicatoinClass as the application class of your app in this line:

android:name=".location.SecondApplicationClass"

so your FirstApplicationClass is not introduced as the application class of your app and you can not get it's ApplicationContext.

change your AndroidManifest.xml code to this and you will be able to get ApplicationContext in your FirstApplicationClass:

<application
android:name=".location.FirstApplicationClass  "
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"

then you can consume the ApplicationContext in both case.

Andronymous
  • 115
  • 1
  • 9
  • But since public class SecondApplicationClass extends FirstApplicationClass, the second application class needed to be mentioned in the manifest right? I'm getting an error when I changed the manifest. error FirstApplicationClass cannot be cast to com.location.SecondApplicationClass because I'm using - mSecondApplicationClass = (SecondApplicationClass) getActivity().getApplicationContext(); – MIDHUN CEASAR Apr 13 '18 at 05:39
  • @MIDHUNCEASAR you should use just one class as the application class in your application. if you just need the `applicationContext`you can get it in `FirstApplicationClass`'s `onCreate()` method and store it as a static object so you will be able to use it in any class. then create two different classes to imlement `GoogleApiClient`'s callBack and implement methods to get and store location and when is needed you can use the static context you stored in `FirstApplicationClass` – Andronymous Apr 15 '18 at 05:38