I'm new to Android. Can someone explain me the concept of Context class/Object. What it is? What it will be used for? Why Context class?
Have you seen the android developer's guide? it will answer your questions:
Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.
A Context has a lot of functions, but as a developer, you primarily use it to load and access application resources.
In programming Android applications, you will hardly ever need to use the Context class directly (not possible at all since Context is abstract), but you will need the child classes that derive from it like Activity, Service etc.
You might want to look these up.
You can think of Context, like the end-user-interface that will use that code. When you are in a class you can know based on Context if you have visual screen(Activity), or a running service(Service).
To compare against some other programming example, you can think of Context is equal to Console App, GUI App, or even Applet.
I hope if you read this source code you will have answer for the problems:
http://www.devdaily.com/java/jwarehouse/android/core/java/android/content/Context.java.shtml
Related
I was reading several guides and one book about the Strategy pattern. I noticed that in all the guides there is a referall to a Contextclass that favoring delegation over implementation decide for some specific behaviors.
As I manage in android the class, object Context on daily basis, I would like to know if the SDK engineers tought at the Android Contextas a concrete (complex) implementation of the strategy pattern.
As Wikipedia says, the Strategy design pattern enables selecting an algorithm at runtime (where multiple possible implementations exist). The Context class does not encapsulate any single algorithm. And for the services that it allows to access, there are no multiple possible implementations selected at runtime; there's just a single implementation provided by the OS.
Therefore, no, the Context class has nothing in common with the Strategy pattern.
Design patterns represent the best practices used by experienced object-oriented software developers. It is naive to believe in a platform like Android this is not applied. For example, a design pattern when designing Android Context would be when dealing with different screen sizes. The context has instruction based on a Strategy Design Pattern to deal with so many different screen sizes.
I found this to be the simplest and most useful tutorial for design patterns with example of using context class.
https://www.tutorialspoint.com/design_pattern/index.htm
Basically, Context is reference that holds global information about an "Application environment".
This is an abstract class whose implementation is provided by the Android system.
It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.
In short, it's reference about things running inside application (you can assume it as global pointer to application process registry to be used in application environment, *in my opinion).
You can check out more here.
I have the following in my application (com.para.app1):
<com.para.app2.MyKeyboardView
...
which is a custom view. The implementing class (I.e. MyKeyboardView) is defined in another application (com.para.app2) which is already installed on my device.
Although I have used a fully qualified class name in my layout.xml (that's "com.para.app2.MyKeyboardView"), every time I run my app, I face ClassNotFoundException for MyKeyboardView.
Any idea?
Just to let you know about the error, the issue is that your applications are running in their own processes and VM instance.
Now to answer your question, It is possible, but it would be a dumb idea to take the time to do so since it might have an important impact in performance, either way you might be able to accomplish it by making both apps share the same process(which is possible if both apps are signed with the same key only)but I would strongly recommend you to have the class in both applications and avoid all that useless overload on your apps.
Anyway if both apps are using same process you can use Context.createPackageContext() to instantiate a package for the other app as long as both apps are using the same shared user ID, then you can get the ClassLoader from the context and instantiate whatever classes you want.
Regards!
No. You must have all the classes your app uses in your apk. The only way to run code in another apk is via an Intent. Of course you can't use an Intent to make a view.
I saw several places and people suggesting extending the Application class in their app.
Personally, I have never needed the necessity to do so.
I was not able to find a clear cut and direct answer by google-ing, so here are some questions :
What is an application class ?
why do we need it ?
Is it a singleton ? or we can have several instances ?
When we use it inside our manifest, is it the same as using the default application that usually eclipse generates ?
When are cases that extending it might be needed ? and when it is an overhead ?
Is it useful for caching purposes ?
It is something I have also thought about when I first started developing with Android and never came up with an answer. However just a quick google myself revealed this website.
Essentially I think it comes down to the fact if you are making a more involved Android application which would involve several Activity classes then things become difficult if you need to persist data that is to be shared between the various Activity classes of your application. Quoting from the above website:
If you take a look at the Application class (see here) in the API reference guide, you get a suggestion on how to manage global application data. The Application class, it says, is the “Base class for those who need to maintain global application state.”
If you've just started Android development this point may not be apparent to begin with however once you've worked with SDK for a while and you come up with an idea that you'd like to develop in a serious way you may hit upon this problem and then the reason for the Application class will become clear.
In the Android documentation there is quite good explanation what the Applications.class is.
Base class for those who need to maintain global application state. You can provide your own implementation by specifying its name in your AndroidManifest.xml's tag, which will cause that class to be instantiated for you when the process for your application/package is created.
There is normally no need to subclass Application. In most situation, static singletons can provide the same functionality in a more modular way. If your singleton needs a global context (for example to register broadcast receivers), the function to retrieve it can be given a Context which internally uses Context.getApplicationContext() when first constructing the singleton.
Sometimes you may need it to extend in order to work with external libraries. I did use it for UrbanAirship see here
The Application instance is indeed one for our app and it is created when our application starts and removed when it is closed. It represents general state of the application.
Just started today to learn Android development, and I just can't find any info on how may I define a Helper class, or a collection of functions that will load globally and I'll be able to use them in any Activity that I create.
What I'm planning, is to create (at least for now) 2 functions that will be used almost in every activity, and would be a mess having to define the same functions in every activity.
Which would be the proper way to approach the above?
I want to throw out a quick note that you should not be putting functions in a global application class for this purpose. Generally what happens is that if you have shared functionality, you abstract that and put the code in:
Static methods
Another package
A library implementing the functionality against which you link your app
A content provider
You should explicitly not implement this functionality in the Application class. There are uses for extending the application, but doing so results in code smell and is a common antipattern for beginners. If you want a bunch of functionality which is shared in your app, a library is the perfect solution (even if it's just code you separate into another package). This encourages reuse and allows the code to be more cleanly segmented and tested.
It's common in Java to have either a class or pacakge of the form *.util for this.
(I'm guessing you come from something like a Rails world where this is the norm, since Ruby is much more metaprogramming oriented than Java, you generally use different tricks.)
This would be a static method in Java.
Java Static Methods
In a class Helper,
public static void DoSomething(){
}
Would be accessible from any class.
Example:
Helper.MyFunction();
How do I make application context available to all classes in application in the app?
My supporting classes need to be able to access it in order to access resourses.
Best answer: pass in a Context to your supporting classes' methods, as the application context may or may not work for your particular cases. You will notice, for example, that many supporting classes in Android take this approach, and it is generally a good idea to follow the patterns set forth by the platform developers.
Possibly tolerable answer: use a static data member to hold a reference to the Application object. Be very very careful that you do not introduce memory leaks in the process.