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

Android

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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()
    }
 }

Solution 3 - Android

Casting a Context object to an Activity object compiles fine.

Try this:

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

Solution 4 - Android

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

Solution 5 - Android

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>

Solution 6 - Android

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

Solution 7 - Android

try this, calling the activity in the constructor

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

Solution 8 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRajView Question on Stackoverflow
Solution 1 - AndroidmgvView Answer on Stackoverflow
Solution 2 - AndroidChiragView Answer on Stackoverflow
Solution 3 - AndroidSome Noob StudentView Answer on Stackoverflow
Solution 4 - AndroidRobby PondView Answer on Stackoverflow
Solution 5 - AndroidLapenkov VladimirView Answer on Stackoverflow
Solution 6 - AndroidRClemensView Answer on Stackoverflow
Solution 7 - AndroidIssac BalajiView Answer on Stackoverflow
Solution 8 - AndroidLeeView Answer on Stackoverflow