Created
May 4, 2012 09:28
-
-
Save z8888q/2593576 to your computer and use it in GitHub Desktop.
Extending the Android Application class and dealing with Singleton
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.devahead.extendingandroidapplication; | |
import android.app.Application; | |
public class MyApplication extends Application | |
{ | |
@Override | |
public void onCreate() | |
{ | |
super.onCreate(); | |
// Initialize the singletons so their instances | |
// are bound to the application process. | |
initSingletons(); | |
} | |
protected void initSingletons() | |
{ | |
// Initialize the instance of MySingleton | |
MySingleton.initInstance(); | |
} | |
public void customAppMethod() | |
{ | |
// Custom application method | |
} | |
} | |
// | |
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.devahead.extendingandroidapplication" | |
android:versionCode="1" | |
android:versionName="1.0"> | |
<uses-sdk android:minSdkVersion="7"/> | |
<application android:icon="@drawable/icon" android:label="@string/app_name" | |
android:name="com.devahead.extendingandroidapplication.MyApplication"> | |
<activity android:name=".MainActivity" | |
android:label="@string/app_name"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN"/> | |
<category android:name="android.intent.category.LAUNCHER"/> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> | |
// | |
package com.devahead.extendingandroidapplication; | |
import android.app.Activity; | |
import android.os.Bundle; | |
public class MainActivity extends Activity | |
{ | |
protected MyApplication app; | |
@Override | |
public void onCreate(Bundle savedInstanceState) | |
{ | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
// Get the application instance | |
app = (MyApplication)getApplication(); | |
// Call a custom application method | |
app.customAppMethod(); | |
// Call a custom method in MySingleton | |
MySingleton.getInstance().customSingletonMethod(); | |
// Read the value of a variable in MySingleton | |
String singletonVar = MySingleton.getInstance().customVar; | |
} | |
} | |
// | |
package com.devahead.extendingandroidapplication; | |
public class MySingleton | |
{ | |
private static MySingleton instance; | |
public String customVar; | |
public static void initInstance() | |
{ | |
if (instance == null) | |
{ | |
// Create the instance | |
instance = new MySingleton(); | |
} | |
} | |
public static MySingleton getInstance() | |
{ | |
// Return the instance | |
return instance; | |
} | |
private MySingleton() | |
{ | |
// Constructor hidden because this is a singleton | |
} | |
public void customSingletonMethod() | |
{ | |
// Custom method | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment