Android Application Class


Application class is the base class where you can maintain global variables.
 One can override the Application class by extending his own class  and 
specifying that class in AndroidManifest.xml’s tag.
Application class exists through out the app life cycle.
 Creating a static singletons can serve the purpose instead of extending and overriding Application class.
 But if one needs to manage other memory leaks he can override Applications class methods.import android.R.string;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;

public class myAppClass extends Application {
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
} @Override
public void onCreate() {
super.onCreate();
}

@Override
public void onLowMemory() {
super.onLowMemory();
}

@Override
public void onTerminate() {
super.onTerminate();

}

}

this is lifecycle code in Android Programming
main advantage of application class is , you can access global variable from anywhere in your project without loss, you may ask that we can accomplish this using activity , but difference is when you move from one activity to another one , there is difficult in that, in that case if u extend Application class, you can do that process as follows
public class myAppClass extends Application {
   
        public static String globalWord = “HI DEVS”;
        private static myAppClass Singleton;
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
} @Override
public void onCreate() {
      super.onCreate();
              Singleton = this;   // create singleton
}
public static myAppClass getSingleton()  // access singleton
{
        return Singleton;
}
}


Note:  add this class in  AndroidManifeist file as below
<application
        android:name=”.myAppClass”
        android:icon=”@drawable/ic_launcher”
        android:label=”@string/app_name”
        android:theme=”@style/AppTheme”
        >

       

Feedback and comments expexted as usual


Leave a Reply

Your email address will not be published. Required fields are marked *