Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android library project - how to get context?

Tags:

android

I have been happily refactoring code from different versions of the same app (paid/free) into Android library projects so that the actual apps can simply customize the library and reduce code duplication.

One thing I'm started to wonder is what getApplicationContext() inside the library code means? Is is the same ApplicationContext as one would get from the child apps? What happens when I access SharedPreferences from a library project's getApplicationContext() instead of the original app's getApplicationContext()? Will the SharedPreferences file be same or different?

What if I had used the activity to access SharedPreferences? Does it matter that the activity is now a library activity and not the original app? Is the SharedPreferences the same?

Thanks for clarifying.

like image 240
amit Avatar asked Feb 07 '12 11:02

amit


People also ask

How do you get the context inside the companion object?

A) Provide the context as an argument to all functions that need it. B) Instead of a Singleton companion object, create an instance of the class and provide the context as a constructor argument. Consider closing / cleaning up the instance, when not used anymore.

How do I find the context of an object?

To access the context object, first specify the parameter name by appending . $ to the end, as you do when selecting state input with a path. Then, to access context object data instead of the input, prepend the path with $$. .

How do I find the context of an app?

Do this: In the Android Manifest file, declare the following. Now everywhere call MyApplication. getAppContext() to get your application context statically.

What is Androd context?

A Context is a handle to the system; it provides services like resolving resources, obtaining access to databases and preferences, and so on. An Android app has activities. Context is like a handle to the environment your application is currently running in. The activity object inherits the Context object.


2 Answers

When the APK is packaged up then all classes will be belong to the main application.

call getApplicationContext().getPackageName() and it will return the app's package name, and not the library's package.

I have the same setup for a free/paid application and no issues when I moved my classes into a library project.

However you have to check your xml files (manifest, widgets, etc.) to use the full package name of your library project.

like image 130
ramdroid Avatar answered Sep 28 '22 12:09

ramdroid


A library project is almost like having all the code in one project. There are a couple of things to watch out for related to namespaces but generally it works very well.

e.g. Your library has its own namespace

Library package name = uk.co.lib Main App package name = uk.co.app

Activities in the library that you want tro access from the main app have to be added to the app manifest. Activity named A in library project would be added to manifest in main app like this:

<activity android:name="uk.co.lib.A">

Accessing shared preferences etc would give the same result from either namespace and would return the preferences for the app.

There is only one application so there is only one ApplicationContext

like image 42
Kuffs Avatar answered Sep 28 '22 12:09

Kuffs