Raj
Raj

Reputation: 6830

android: how to use getApplication and getApplicationContext from non activity / service class

I'm using extending application class on Android to share my data across the entire app.

I can use getApplication() method from all my activities.

However, there are certain custom helper classes I created; for example, an XMLHelper class which does not inherit from any activity / service class.

Here the getApplication() method is not available.

How do I sort this out and what are the best design practices to solve this?

Upvotes: 55

Views: 133738

Answers (8)

Lee
Lee

Reputation: 11

((MyApplication) getApplicationContext()).myMethod()

Upvotes: -1

Lapenkov Vladimir
Lapenkov Vladimir

Reputation: 3218

In order to avoid to pass this argument i use class derived from Application

public class MyApplication extends Application {

private static Context sContext;

@Override
public void onCreate() {
    super.onCreate();
    sContext=   getApplicationContext();

}

public static Context getContext() {
    return sContext;
}

and invoke MyApplication.getContext() in Helper classes.

Don't forget to update the manifest.

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example"> 
      <application 
       android:name=".MyApplication"
       android:allowBackup="true" 
       android:icon="@mipmap/ic_launcher" 
       android:label="@string/app_name"> 
         <activity....>
            ......
         </activity>
     </application> 
</manifest>

Upvotes: 5

Issac Balaji
Issac Balaji

Reputation: 1441

try this, calling the activity in the constructor

public class WebService {
private Activity activity;

        public WebService(Activity _activity){
            activity=_activity;
            helper=new Helper(activity);

        }
}

Upvotes: 0

Some Noob Student
Some Noob Student

Reputation: 14574

Casting a Context object to an Activity object compiles fine.

Try this:

((Activity) mContext).getApplication(...)

Upvotes: 22

mgv
mgv

Reputation: 8484

The getApplication() method is located in the Activity class, that's why you can't access it from your helper class.

If you really need to access your application context from your helper, you should hold a reference to the activity's context and pass it on invocation to the helper.

Upvotes: 60

Chirag
Chirag

Reputation: 56925

The getApplication() method is located in the Activity class, so whenever you want getApplication() in a non activity class you have to pass an Activity instance to the constructor of that non activity class.

assume that test is my non activity class:

Test test = new Test(this);

In that class i have created one constructor:

 public Class Test
 {
    public Activity activity;
    public Test (Activity act)
    {
         this.activity = act;
         // Now here you can get getApplication()
    }
 }

Upvotes: 27

RClemens
RClemens

Reputation: 301

Sending your activity context to other classes could cause memoryleaks because holding that context alive is the reason that the GC can't dispose the object

Upvotes: 2

Robby Pond
Robby Pond

Reputation: 73484

Either pass in a Context (so you can access resources), or make the helper methods static.

Upvotes: 4

Related Questions