Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get application's context in an Android Library Project?

I would like to get the context of application which has reference/hosted my library at run-time inside one class of my library project. Is it possible? If yes, how?

Thanks

Update I don't want my user to pass context in parameter to my library project because it is possible that my library project will be called through JNI and I have no idea how I can get context in JNI and pass it to Java layer.

like image 456
Haris Hasan Avatar asked Aug 23 '11 07:08

Haris Hasan


People also ask

How do I get application context from activity?

You can go for getApplicationContext() if you wanna get context of whole application. If you want to get context of current class you can use getBaseContext() instead.

Can Android library have application class?

You can extend the library's Application class in the application project and provide any additional implementation.

How many types of context are there in Android?

There are mainly two types of Context that are available in Android.

Which of the following are the two types of context in Android?

Application Context: It is the application and we are present in Application. For example - MyApplication(which extends Application class). It is an instance of MyApplication only. Activity Context: It is the activity and we are present in Activity.


2 Answers

There is one more way, add application class in your library project:

/**  * Base class for those who need to maintain global application state.  */ public class LibApp extends Application {    /** Instance of the current application. */   private static LibApp instance;    /**    * Constructor.    */   public LibApp() {     instance = this;   }    /**    * Gets the application context.    *     * @return the application context    */   public static Context getContext() {     return instance;   }  } 

Then in your regular project make the real application class extend LibApp:

/**  * Base class for those who need to maintain global application state.  */ public class App extends LibApp {    @Override   public void onCreate() {     super.onCreate();   }  } 

Make sure that you have "Name" defined in AndroidManifest:

<application android:name="App" ...> 

and that your App class is in the base package.

You can then use LibApp.getContext() your library project to get the application context of the application that is using the library.

This may not be good solution but it works for me. I am sharing it because it might be useful to somebody else.

like image 97
peceps Avatar answered Oct 07 '22 21:10

peceps


Is it possible?

Yes.

If yes, how?

Pass it in as a parameter.

I don't want my user to pass context in parameter to my library project because it is possible that my library project will be called through JNI and I have no idea how I can get context in JNI and pass it to Java layer.

Then figure out "how [you] can get context in JNI and pass it to Java layer". I would imagine that you would pass it like any other object. As @Blundell noted, you do not really have any other option.

like image 26
CommonsWare Avatar answered Oct 07 '22 20:10

CommonsWare