Skip to content Skip to sidebar Skip to footer

Application Singleton Use In Android

I have a facebook initialize sdk call and I need it to initialize the moment application is launched: I want to use my Application class to do that. for example: public class App e

Solution 1:

When you extend android.app.Application class, you do not have to call onCreate explicitly. But make sure you specify it in AndroidManifest.xml, like this android:name="fully-qualified name" eg: android:name="com.example.MyApplication"

You don't have to implement Singleton pattern cause Application IS ALREADY A SINGLETON, there is only one instance of your Application. And, to get the instance of Application or the any custom child of Application you defined, you can simply call context.getApplication().

Reference https://developer.android.com/reference/android/app/Application.html

Solution 2:

To make your App class singleton follow the Singleton design pattern:

publicclassApp
  {
    // Create the instanceprivatestatic App instance;
    publicstatic App getInstance()
    {
         if (instance== null) {
                synchronized(App.class) {
                        if (instance == null)
                                instance = new App();
                        }
                }
                // Return the instancereturn instance;
    }

    privateApp()
    {
        // Constructor hidden because this is a singleton
    }

    publicvoidinitFBSdk()
    {
        FacebookSdk.sdkInitialize(getApplicationContext());
    }
}

Then to use it in any other class:

App.getInstance().initFBSdk();

If this is what you are asking for..

Solution 3:

I think you are trying to make a thread safe singleton in java. Here is the code that you need for our App class:

publicclassAppextendsApplication 
{
    privatestaticApp instance;

    publicstaticAppgetAppInstance() { return instance; }

    @OverridepublicvoidonCreate() 
    {
        super.onCreate();
        instance = this;
    }   

    publicvoidinitFacebookSdk()
    {
        FacebookSdk.sdkInitialize(getApplicationContext());
    }
}

This only makes a reference to itself. We want to implement a Singleton for Multithreading. Then we will create another class that encapsulates the App class in its constructor, like this:

publicclassSingletonApp
{
    privatestaticAppappInstance=null;
    privatestaticSingletonAppinstance=null;
    privatestaticfinalObjectmutex=newObject();

    publicstatic SingletonApp getInstance() 
    {
        SingletonAppr= instance;
        if (r == null) {
            synchronized (mutex) {  // While we were waiting for the sync, another 
                r = instance;       // thread may have instantiated the object.if (r == null) {  
                    r = newSingletonApp();
                    instance = r;
                }
            }
        }

        return r;
    }

    privateSingletonDemo()
    {
       // Direct access the Application context calling
       appInstance = App.getAppInstance();
    }

    publicstaticgetAppInstance()
    {
       return getInstance().appInstance;
    }
}

Then in your MainActivity class you can import the static instance and use it on your code:

importstatic your.package.name.SingletonApp.getAppInstance;

For example, you can use it in your MainActivity class just calling the method initFacebookSdk() like this:

publicclassMainActivityextendsAppCompatActivity
{
    ...

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initFacebookSdk();

        ...
    }
}

Post a Comment for "Application Singleton Use In Android"