Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use and not to use the android Application class?

I am considering using the android Application class as a place to store temporary state and common code shared by other (fragment) activities in the app.

I would like to get more feedback as to whether it is a good place for:

  1. Shared constants like ID's, pref key names, etc.
  2. Global variables (i.e. setters/getters) reflecting current UI state, navigation, selected fragment, and, in general, temporary data that does not need to be persisted.
  3. Hooks for persisting data when certain conditions are triggered.
  4. Updating the UI after preference changes.
  5. Providing an easy way to access the context from anywhere in the app, including code where getApplication() is not available, e.g. via a static getter such as MyApp.getApp().
  6. Common methods that need the visibility of the global state variables and which would become too cumbersome to move away to dedicated classes.

What else would be appropriate/useful/handy to have in the activity class? What would not be a good idea to keep in it and what would be the best alternatives? And finally, what you have found Application to be best used for in your apps?

like image 370
ccpizza Avatar asked Jan 07 '13 14:01

ccpizza


People also ask

Why do we use 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.

What is the advantage of Android application?

Advantages of Android application developmentUsers like to customize their applications, if possible, and for Android application development this process is made easier. Platform without licenses Android application development is based on an open source platform.

Why is Android application development your best option?

Android Development is low cost and provides high ROI. Since Android has a huge number of clients from increasingly diverse backgrounds, applications are less expensive. Most of the applications are free to download and are also easily available on the play store in comparison to another operating system.

Can we have application without activity?

Sure! No reason you cannot have an application with only a service. ...and no need to get into AIDL unless you want to. The problem is, how to make the application run. When you create an application with an Activity, you add an Intent filter, in the manifest, that makes the activity startable from the Launcher.


2 Answers

Shared constants like ID's, pref key names, etc.

I generally create a constants file called C for this, as its better for readability. C.SHARED_PREFS is easier to understand that Application.SHARED_PREFS IMHO.

Global variables (i.e. setters/getters) reflecting current UI state, navigation, selected fragment, and, in general, temporary data that does not need to be persisted.

This would be better off in the Activity or component which it concerns (for example, the UI state of an Activity should probably stored in the icicle bundle, or within that instance of the Activity).

Hooks for persisting data when certain conditions are triggered.

This should be fine.

Updating the UI after preference changes.

Again, I feel this would be better off in the respective component.

Providing an easy way to access the context from anywhere in the app, including code where getApplication() is not available, e.g. via a static getter such as MyApp.getApp().

This would work, but be careful of memory leaks. You should generally pass the context in as a parameter when calling the method from an Activity or Service or whatever. Less chances of a memory leak.

Common methods that need the visibility of the global state variables and which would become too cumbersome to move away to dedicated classes.

I feel it would be better to make the effort of dedicated classes, as when your app grows in features and size, this will become difficult to maintain.

like image 131
Raghav Sood Avatar answered Oct 05 '22 22:10

Raghav Sood


It is probably some place where certain hooks can be attached.

For instance, if you use ACRA crash reporting library, you just need to use the Application class, because this is where ACRA is attached. This is that forced me to start using this class; I never needed the one before.

like image 28
Audrius Meškauskas Avatar answered Oct 06 '22 00:10

Audrius Meškauskas