In my app Application class is overrided and used for storing some data. Where needed I'm using ((MyApplication)getApplication()).someMethod() and it working well. But I have one class where Activity is not available. In this class is available the context only. So I cannot call method getApplication().
How can I get Application object without getApplication() method? May be I can get it from Context or via static methods?
Related
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.
I have activity that run and do its work, and then run a service (that will not finish when the activity finish.)
All is ok.
My question: the service uses values that declared in the activity class as static public, does these values reset when the the activity finish and the service continue working and using them?
What happens to these values when the activity start again.
According to Android document
How do I pass data between Activities/Services within a single application?
A public static field/method
An alternate way to make data accessible across Activities/Services is to use public static fields and/or methods. You can access these static fields from any other class in your application. To share an object, the activity which creates your object sets a static field to point to this object and any other activity that wants to use this object just accesses this static field.
I think static will remain in the memory as long as the onDestroy() of the activity is not called. Once it is called static members will be removed from memory.
If you need to persist your data then you can use sharedprefrences.
Activity/Service – inherit from ContextWrapper which implements the same API, but proxies all of its method calls to a hidden internal Context instance, also known as its base context. Whenever the framework creates a new Activity or Service instance, it also creates a new ContextImpl instance to do all of the heavy lifting that either component will wrap. Each Activity or Service, and their corresponding base context, are unique per-instance.
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
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.
I am currently making an application which only has three classes. Two Activities, and an Application class. From what I have learned of Applications so far, the class initializes itself at the start of the program, so does that mean I do not need to initialize an Object of the class in each Activity?
My program crashes at the start every time and is returning a ClassCastException, which I'm assuming has to do with my Application class since it is the only class casting I am doing in all of my code. As a local variable I have
protected BluetoothApplication myBt;
and inside my onCreate() method I call
myBt = (BluetoothApplication)getApplication();
No you don't need to initialise it manually but you can use getApplicationContext() to get the instance of your Application Class for eg:-
MyApplication application = ((MyApplication)getApplicationContext());
You can also access Application class from a Non-Activity class by passing a Context to that class and then using that context for getting the instance of Application class by,
MyApplication application = ((MyApplication)context.getApplicationContext());
Is the BluetoothApplication a custom subclass of Android's default Application-class? If so, then do you tell Android in your AndroidManifest.xml to use that class instead of the default Application class?
See Android Application API for more details.