Saturday, December 8, 2018

 Android Application Class

The Application class in Android is the base class within an Android app that contains all other components such as activities and services.
The Application class, or any subclass of the Application class, is instantiated before any other class when the process for your application/package is created.
Base class for maintaining global application state. You can provide your own implementation by creating a subclass and specifying the fully-qualified name of this subclass as the "android:name" attribute in your
AndroidManifest.xml's <application> tag. The Application class, or your subclass of the Application class, is instantiated before any other class when the process for your application/package is created.

Creating Application Class in JAVA


package com.upasarga.elibrary.helper;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import com.facebook.FacebookSdk;
import com.upasarga.elibrary.constants.SharedPrefConstants;
/**
* Created by NaRan on 23,Sep,2018.
* Copyright © UT Pvt. Ltd. All rights reserved.
* nrn.panthi@gmail.com
**/
public class UpasargaApplication extends Application implements Application.ActivityLifecycleCallbacks {
private static UpasargaApplication upasargaApplication;
private static SharedPreferences sharedPreferences;
private static boolean isActive;
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(this);
upasargaApplication = this;
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getAppContext());
if (sharedPreferences.contains(SharedPrefConstants.SELECTED_LANGUAGE)) {
LocaleHelper.onCreate(getApplicationContext(), sharedPreferences.getString(SharedPrefConstants.SELECTED_LANGUAGE, “”));
} else {
LocaleHelper.onCreate(getApplicationContext(), “en”);
}
initialiseSocialLogin();
}
private void initialiseSocialLogin() {
FacebookSdk.isInitialized();
}
public static boolean isActivityVisible() {
return isActive;
}
public static Context getAppContext() {
return upasargaApplication.getApplicationContext();
}
public static SharedPreferences getSharedPreference() {
return sharedPreferences;
}
@Override
public void onActivityCreated(Activity activity, Bundle bundle) {
}
@Override
public void onActivityStarted(Activity activity) {
}
@Override
public void onActivityResumed(Activity activity) {
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
}
@Override
public void onActivityDestroyed(Activity activity) {
}
}

Creating Application Class in KOTLIN


package com.smartgov.mahilapahila.helper

import android.app.Activity
import android.app.Application
import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import android.preference.PreferenceManager

/**
 * Created by NaRan on 23,Sep,2018.
 * Copyright (c) UT Pvt. Ltd. All rights reserved.
 * nrn.panthi@gmail.com
 */
class Application : Application(), Application.ActivityLifecycleCallbacks {


    init {
        instance = this
    }

    companion object {
        private var instance: Application? = null
        private var sharedPreferences: SharedPreferences? = null

        fun applicationContext(): Context {
            return instance!!.applicationContext
        }

        fun getSharedPreference(): SharedPreferences? {
            return sharedPreferences
        }
    }

    override fun onCreate() {
        super.onCreate()
        // initialize for any

        // Use ApplicationContext.
        // example: SharedPreferences etc...
        val context: Context = Application.applicationContext()

        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext)
    }

    override fun onActivityPaused(activity: Activity?) {

    }

    override fun onActivityResumed(activity: Activity?) {

    }

    override fun onActivityStarted(activity: Activity?) {

    }

    override fun onActivityDestroyed(activity: Activity?) {

    }

    override fun onActivitySaveInstanceState(activity: Activity?, outState: Bundle?) {

    }

    override fun onActivityStopped(activity: Activity?) {

    }

    override fun onActivityCreated(activity: Activity?, savedInstanceState: Bundle?) {

    }


}

Uses of Application Class:

  1. Access to variables across the Application,
  2. You can use the Application to start certain things like analytics etc. since the application class is started before Activitys or Servicess are being run,
  3. There is an overridden method called onConfigurationChanged() that is triggered when the application configuration is changed (horizontal to vertical & vice-versa),
  4. There is also an event called onLowMemory() that is triggered when the Android device is low on memory.

Limitations and Warnings

Storing data in the Application object is error-prone and can crash your app. Prefer storing your global data on disk if it is really needed later or explicitly pass to your activity in the intent’s extras.
You should never store mutable instance data inside the Application object because if you assume that your data will stay there, your application will inevitably crash at some point with a NullPointerException. The application object is not guaranteed to stay in memory forever, it will get killed. Contrary to popular belief, the app won’t be restarted from scratch. Android will create a new Application object and start the activity where the user was before to give the illusion that the application was never killed in the first place.

No comments:

Post a Comment

 Android Application Class Na Ran Dec 8, 2018 The   Application   class in Android is the base class within an Android app ...