Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android application object [closed]

Tags:

android

I have read about application object in android site but I couldn't understand.

What is application object android?
What is the use of application object
When we should use application object

Please explain with example.

Thank you.

like image 470
user861973 Avatar asked Mar 22 '12 09:03

user861973


People also ask

How do I close applications in Android?

Close one app: Swipe up from the bottom, hold, then let go. Swipe up on the app. Close all apps: Swipe up from the bottom, hold, then let go. Swipe from left to right.

When closing an app on Android what is happening in the backend?

When system realizes that it needs to free up memory, Android will start killing the processes which are the oldest and placed at the end of hierarchy. Only in very critical situations will Android get to a point where all Cached processes are killed and it must start killing Service processes.

What is MainApplication Java in Android?

So for me the purpose of MainApplication. java is to serve Android Native Modules. And that the class is declaring how the app works and how it is composed.

Where is the application class in Android?

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.


2 Answers

Application object is an object whose lifecycle is same as our Application.

When Application starts this object is created automatically by Android Framework.

When you want to share data between more than one activities of your application or you want to retain something at application level.In that scenario you can use Application object as a global store for your application.

Example::

// this is your Application Object Class
public class MyApplication extends Application{
}

In your manifest mention it as follows::

<application android:name="yourpkgname.MyApplication"/>

To access application object in any of the activities.Use the following code.

MyApplication app = (MyApplication) getApplication();

I think this is quite enough for you to grab the concept of application object.

like image 181
Ashwin N Bhanushali Avatar answered Oct 23 '22 23:10

Ashwin N Bhanushali


q1. What is an Android application Object?

A. According to developer docs Android application object is

"Base class for those who need to maintain global application state. You can provide your own implementation by specifying its name in your AndroidManifest.xml's tag, which will cause that class to be instantiated for you when the process for your application/package is created"

q2. What is the use of Application Object?

A. The Application class is mainly used for some Application level callbacks and for maintaining Global Application state.

So basically here is an implementational idea

public class MyApp 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();
}

}

q3. When should you use Application Object?

A.When you want to store data, like global variables which need to be accessed from multiple Activities, sometimes everywhere within the application. In this case, the Application object will help you.

like image 35
Arif Nadeem Avatar answered Oct 24 '22 00:10

Arif Nadeem