Where to get Context from standalone native application? - android

A standalone native application should perform calls to Java VM. Creating VM and playing with classes seem to be OK, but there is a problem while obtaining instance of Context. I can not create Application object and use it (NullPointerException is thrown). Also, Context class is abstract and can not be instantiated.
So, where can I get the Context object?.
Thanx.

Activity extends Context, so your Activity IS a Context.

Related

Android getApplicationContext() outside Activity and Application

How I can get application context outside activity and without extending application class.
class A{
public static B b = new B(App context here);
}
Objetc b must be as a field
No you can't get context without extending Application or Activity by the given example. All that you can do is to have a static method in a class (extends Application) that would return context and then pass that method as a parameter to your B().
BTW, I did not get your intention of doing this. Can you detail out what exactly you wants to do.
Thank you
It is not possible.
If it were, it would mean that any Java class would be able to access an android.content.Context instance, even when is not related to anything in Android, for example, a Java EE application.
What you can do, is, have a static reference to a Context (an Application or an Activity) in a centralized place inside your app, but I wouldn't recommend it because it would cause multiple memory leaks.

how does startActivity(this, OtherActivity.class) use Context object to start an activity?

I started reading about Context And I am a bit confused.
I found the following definition.
The Context class is an “Interface to global information about an application environment.”
here I am not understanding what android application environment is?
we can use 1. getAssets() 2. getResources() 3. getPackageManager() 4. getString() 5. getSharedPrefsFile() methods ,we can also start an activity ,broadcast an Intent using Context.
I think,The above methods(bolded) provide app's global data to the app,
Are these Resources known as App global data?
So for starting activity ,what global information is used by Android
System?
How does a View use Context (what information it gets from Context)?
I am so much confused about the concept of Context class and how all these things are related.
Please provide any help.
Please go through Context details. I think these things can help you understand the idea of context.
View.getContext():
Returns the context the view is currently running in. Usually the currently active Activity.
Activity.getApplicationContext():
Returns the context for the entire application (the process all the Activities are running inside of). Use this instead of the current Activity context if you need a context tied to the lifecycle of the entire application, not just the current Activity.
ContextWrapper.getBaseContext():
If you need access to a Context from within another context, you use a ContextWrapper. The Context referred to from inside that ContextWrapper is accessed via getBaseContext().

What is the purpose of Application class in Android

What exactly is the purpose of Application class.
what are the benefits of extending it to a custom subclass
Why use it ?
Can global variables be stored in any other class achieve same goal as Application ?
Nice question !
Your application is a context that is always running while your activities and services are running.
It is also the first context to be created and the last to be destroyed.
Thus, it surrounds the life cycle of your app.
You can use the application class as a way to share data or components (for dependency injection for instance). For instance if you want to share a singleton between activities, you can create the instance in the application class and provide a getter, then all other contexts can get the singleton via
((cast to your class)getApplicationContext()).getFoo();
There may be some use cases where you need to do stuff before even your first activity is launched, then do it in the onCreate method of the application class.
On the other hand, you should never relie on the onDestroy method of the Application class, as it is not always called. There is no contract for that on Android.
But this is rare and, usually, you don't need to override the application class though. Dependency injection can be achieved in other ways by RoboGuice or Dagger for instance.
Two things makes this Class very useful:
Application class is instantiated before any other Activity.
It holds the Application Context
Context brings a host of resources for us: we can figure out some device properties, load some resources, initiate a SQLite database etc, etc.
All of this happens before any Activity loads, and all of this is globally available to the Activities.
Simple example of what I mean:
public class App extends Application{
private static Resources sResources;
//--I want to load strings resources from anywhere--
public static String loadStringResource(int resID) {
return sResources.getString(resID);
}
#Override
public void onCreate() {
super.onCreate();
sResources = getResources();
//---I want to load all preferences when my app starts---
PreferenceManager.setDefaultValues(this,R.xml.prefs,false);
}
}
Extending the Application class allows you to integrate into the application's lifecycle.
This is also useful to store global application-level information (though it's usually good to keep your activities 'independent')
The Application class is aware of the Application Context and is loaded when your app is loaded so it holds the proper callbacks for the application lifecycle before your activity starts. You most likely would not want to extend this class.
From the API docs:
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.
http://developer.android.com/reference/android/app/Application.html

Is there any initialization callback of process in android?

Suppose that my application has many processes.
I try to run some init code (Actually store the application context in a static variable) in Application onCreate(). But I find that in some process, onCreate() is not run before other code in the same process and hence can not access the cached Context.
Where should I put the init code (store the application context) so that it must be ran before any code in the same process?
Based on the discussion we had in chat, you have a problem because you are trying to get the application's Context during static initialization of some helper class. I suggested the following:
If your helper class is only used by Android components
then by the time an Android component calls your class
the Application.onCreate() will have been called
and you can get the context from there.
You just need to wait until you get an actual call from an Android component to go and get the Context.
Don't try to do it as soon as the helper class is instantiated. Only do it when you need it.

Android: how to get Context when testing with ActivityInstrumentationTestCase2?

I'm running a junit test in Android which extends ActivityInstrumentationTestCase2. I'm using this on order to start up an activity.
The activity uses a subclass of the application object to obtain some parameters. I get the application object from the context.
Unfortunately, ActivityInstrumentationTestCase2 does not provide access to the context. Is there a way to access the context before getting the activity?
You can get the application context from the instrumentation object:
getInstrumentation().getTargetContext().getApplicationContext()
To be able to inject an Application using setApplication() you should use ActivityUnitTestCase, as it is only available in this test case class.
By default, ActivityUnitTestCase, creates a hidden MockApplication object that is used as the application under test.
For those using
AndroidTestCase
and needing the app application subclass:
MyApplication context = (MyApplication) getContext().getApplicationContext();

Categories

Resources