Extending and Using the Application Class

Write App Reviews

Build your own Android App Dev Empire

Get Instant Access

Listing 3-7 shows the skeleton code for extending the Application class and implementing it as a singleton.

LISTING 3-7: Skeleton application class

Available for download on

Available for download on

LISTING 3-7: Skeleton application class import android.app.Application;

Wrax.com import android.content.res.Configuration;

public class MyApplication extends Application {

private static MyApplication singleton;

// Returns the application instance public static MyApplication getInstance() { return singleton;

@Override public final void onCreate() { super.onCreate(); singleton = this;

Once created, you must register your new Application class in the manifest's <application> node, as shown in the following snippet:

<application android:icon="@drawable/icon" android:name="MyApplication">

Your Application implementation will by instantiated when your application is started. Create new state variables and global resources for access from within the application components:

MyObject value = MyApplication.getInstance().getGlobalStateValue(); MyApplication.getInstance().setGlobalStateValue(myObjectValue);

This is a particularly effective technique for transferring objects between your loosely coupled application components, or for maintaining application state or shared resources.

Was this article helpful?

0 0
Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide

Post a comment