Android Singleton Method - android

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/

Related

Android: extending Application class. Why do we need to implement singleton pattern?

I'd like to put my database connection object to Application class. I read some tutorials and everywhere I see explicit implementation of a singleton pattern. Something like this
public class MyApplication extends Application {
private static MyApplication singleton;
public static MyApplication getInstance(){
return singleton;
}
#Override
public void onCreate() {
super.onCreate();
singleton = this;
}
}
My question is why do I need to do this explicit implementation? Nobody explains.
I'd like to add there some property, initialized in onCreate() and get it's value in activity.
myProp = (MyApplication)getApplicationContext().getMyProperty()
Is it really necessary to implement singleton pattern?
An Application class is an access point to application context and generally it is used as a initializator for all application-scoped dependencies like your database object. This class is initialized only once per application and persists in memory until application is no longer in memory. So it is a natively created singleton.
By having such static access point to application you may have access to the application context in any class and in general case this context is much better for retrieving resources, system services etc. when you need such dependencies in your custom classes, because it doesn't hold a link to the activity and view so is a leak-safe. Of course in your example the Activity has an access to application, but you have to cast context anyway and better use same approach for a whole app.
Despite you may use it like a global access point to all application scoped and initialized dependencies, do not overload it with huge initialization logic, better create some other singletons for this purpose and just initialize it from Application class. In a big project consider using DI framework, Dagger the best one, for providing dependencies.
By default the application class is singleton..You are not have allowed to create application yourself.Android System will create this when the first time app is loaded into the memory.You are using singleton pattern here just to get Application object from anywhere within the application

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

Application initialization

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.

Using static instead of parcelable/serialize -- is this bad?

I need to keep a reference to an object between different activities. The two said mentions seem to create new objects from activity to activity. This is a problem because changes to the object in the "child" activities don't persist once the parent activity gets focus. My solution to this was just to create a static getter in the parent-most activity which the child activities call and work with. Is this the incorrect way to go about this?
If you want to share a single instance of an object between activities, you can create a singleton class. The only thing wrong with using your parent-most activity class to implement the singleton might be that it might violate the single responsibility principle.
You can make your object persistent throughout the whole application lifecycle by making it a field in your Application-derived class.
public class MyAppication extends Application {
private Object mMyData;
public setData(Object data) {
mMyData = data;
}
public Object getData() {
return mMyData;
}
}
Then ((MyApplication)getAppllication()).setData or getData()
This way you can exchange data within the application because MyApplication will always exist.
You'll also have to add MyApplcation to manifest
You should create a Singleton, this has a single instance whenever you talk to it. (Just like your describing).
Here's one I made earlier : https://stackoverflow.com/a/6539080/413127

Passing complex object to an Activity

I'm working with a Bluetooth app, and I have a thread listening to input messages, and 2 activities: main menu, and bluetooth console. I'm making the connection in the menu Activity, and then I need to pass some objects to the console Activity:
a thread reference to use its write() method
the Context from main Activity
and a boolean I need to check eventualy
I read about using Intent.putExtra and passing a serializable o parceable class object. I wrote a class with the objects I need to pass, but I don't know how to do this, or even what serializable and parceable means.
Isn't there any easy method to pass these objects from an Activity to another?
You could extend Application and put the shared objects into that class. Something like:
public class MyApplication extends Application {
private boolean myBoolean;
public boolean getMyBoolean() { return myBoolean; }
}
You can put anything here you want to use a global variables.
To set the Application of your app to MyAPplication, use the AndroidManifest.xml
<application ... android:name=".MyApplication">
In short answer. No. Activities are isolated from each other because they can span processes so the only portable option is to serialize them. But you can use libraries like Flexjson to serialize plain old Java Beans across using Intent.putExtra(). Much simpler than hand writing serialization with Parcels. Check out: http://flexjson.sourceforge.net
There is a bug in Android that you have to do the following:
/** Fix for Android bug http://code.google.com/p/android/issues/detail?id=5697 */
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());

Categories

Resources