Java Question Quick question about context ?

dealsmonkey

Active Member
Licensed User
Longtime User
Hi everyone,


If an java void is as follows, what is the correct way to access parameter from a ba library ?


xxxx.register(context as Context, String as String)


Would it be ba.context as passed into the void from b4a, ie ?


B4X:
private BA ba;
public void b4atest(BA ba, String eventName){

          this.ba = ba;      
     Context context = ba.context;   
          xxxx.register(context as Context, String as String)

}

Hope this is clear !!

neil
 

agraham

Expert
Licensed User
Longtime User
There are different sorts of BA instances with different fields initialised. A Process BA gets passed to a non-activity object and an activity BA is passed to an activity object. The following applies to library objects declared in activities.

In an activity BA context is a Context and is a reference to the current Activity itself, which inherits from Context. applicationContext is a static field in the BA class and is an Application which is a reference to the Application that owns the activity. It is obtained from activity.getApplication().

In a process BA passed to an non-activity object context is a Context and is a reference to the context of the single, global Application object of the current process. This is obtained from activity.getApplicationContext() and needs to be used with care, see the Android documentation for ContextWrapper. applicationContext being a static field is as above having been set by the first activity or service run in the application process.
 
Top