Application initialization - android

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.

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 to get Application without activity or service?

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?

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

Android Singleton Method

So I'm trying to make an application using the Singleton Method. I want to have a class that stores all the information about my device's bluetooth state/connections/devices, and I want to make multiple activites that can access these methods.
I know that I need to have a class that extends Application, then I can access everything by calling getApplication(). What I do not understand, is where I initialize this object. From my frame of reference, I have all of these separate Activities, and if I initialize the object in one, I'm going to need to use intents to pass the object to the next activity, which completely defeats the purpose of using the singleton method.
Any help would be appreciated, thanks.
Simply extend from android.app.Application. Then register it as the application class in your AndroidManifest.xml:
<application android:name="mypackage.MyApplication" ...>
In your class you will receive usual Android calls, such as in
#Override
public void onCreate() { }
where you will able to initialize your global instances.
In the activities fetch the instance of MyApplication downcasting with:
MyApplication app = (MyApplication) getApplication();
Hope that helps.
If you extended Application your class will be created as your app launches. It can be retrieved in Activity classes using getApplication()
Check here : http://www.kodejava.org/examples/12.html
and here : http://www.devahead.com/blog/2011/06/extending-the-android-application-class-and-dealing-with-singleton/
and here : http://inchoo.net/mobile-development/android-development/android-global-variables/

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