I have a compound control that needs to access the Application object. My control extends LinearLayout, so because it isn’t an Activity I can’t call getApplication(). Is there a way I can do this from a Layout/View or pass the Application in?
I have a compound control that needs to access the Application object. My control
Share
There are a couple of things you could be depending on what you need the Application object for.
If you need the specific application instance, you could try casting your
Contextobject to anActivity:The code above checks to make sure that the Context passed to your custom view is an Activity, but in reality this should always be the case.
If you only need your Application object to use as a `Context’, then you can call the ‘context.getApplicationContext()’ method: