Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The question as to which method is better largely depends upon what information you are storing and need access to and who (which components, packages, etc.) needs access to that information. Additionally, settings like <code>launchMode</code> and <code>configChanges</code> which alter the lifecycle can help you to determine which method is best for you.</p> <p>First, let me note, that I am a huge advocate for extending the Application object and often extend the Application class, but take everything stated here in its context as it is important to understand that there are circumstances where it simply is not beneficial.</p> <p><strong>On the Lifecycle of an Application:</strong> Chubbard mostly correctly stated that the Application has the same life as a Singleton component. While they are very close, there are some minute differences. The Application itself is TREATED as a Singleton by the OS and is alive for as long as ANY component is alive, including an AppWidget (which may exist in another app) or ContentResolver. </p> <p>All of your components ultimately access the same object even if they are in multiple Tasks or Processes. However, this is not guaranteed to remain this way forever (as the Application is not ACTUALLY a Singleton), and is only guaranteed in the Google Android, rather than the manufacturer overridden releases. This means that certain things should be handled with care within the Application Object.</p> <p>Your <code>Application</code> object will not die unless all of your components are killed as well. However, Android has the option to kill any number of components. What this means is that you are never guaranteed to have an <code>Application</code> object, but if any of your components are alive, there IS an <code>Application</code> to associate it to.</p> <p>Another nice thing about <code>Application</code> is that it is not extricably bound to the components that are running. Your components are bound to it, though, making it extremely useful. </p> <p><strong>Things to Avoid in Application Object:</strong> </p> <ul> <li>As per ususal, avoid static <code>Context</code>s. In fact, often, you shouldn't store a <code>Context</code> in here at all, because the <code>Application</code> is a <code>Context</code> itself. </li> <li>Most methods in here should be static, because you are not guaranteed to get the same <code>Application</code> object, even though its extremely likely.</li> <li>If you override <code>Application</code>, the type of you data and methods store here will help you further determine whether you need to make a Singleton component or not.</li> <li><code>Drawables</code> and its derivatives are the most likely to "leak" if not taken care of, so it is also recommended that you avoid references to <code>Drawables</code> here as well.</li> <li>Runtime State of any single component. This is because, again, you are not guaranteed to get back the same <code>Application</code> object. Additionally, none of the lifecycle events that occur in an <code>Activity</code> are available here.</li> </ul> <p><strong>Things to store in the Application (over Bundle)</strong></p> <p>The <code>Application</code> is an awesome place to store data and methods that must be shared between components, especially if you have multiple entry points (multiple components that can be started and run aside from a launch activity). In all of my <code>Application</code>s, for instance, I place my DEBUG tags and Log code.</p> <p>If you have a ContentProvider or BroadcastReceiver, this makes <code>Application</code> even more ideal because these have small lifecycles that are not "renewable" like the <code>Activity</code> or <code>AppWidgetProvider</code> and can now access those data or methods.</p> <p>Preferences are used to determine, typically, run options over multiple runs, so this can be a great place to handle your <code>SharedPreferences</code>, for instance, with one access rather than one per component. In fact, anything that "persists" across multiple runs is great to access here.</p> <p>Finally, one major overlooked advantage is that you can store and organize your Constants here without having to load another class or object, because your <code>Application</code> is always running if one of your components is. This is especially useful for Intent Actions and Exception Messages and other similar types of constants.</p> <p><strong>Things to store in Bundle rather than Application</strong> Run-time state that is dependent upon the presence or state of a single component or single component run. Additionally, anything that is dependant upon the display state, orientation, or similar Android Services is not preferrable here. This is because <code>Application</code> is never notified of these changes. Finally, anything that depends upon notification from that Android System should not be placed here, such as reaction to Lifecycle events.</p> <p><strong>And.... Elsewhere</strong></p> <p>In regard to other data that needs to be persisted, you always have databases, network servers, and the File System. Use them as you always would have. </p> <p>As useful and overlooked as the <code>Application</code> is, a good understanding is important as it is not ideal. Hopefully, these clarifications will give you a little understanding as to why gurus encourage one way over the other. Understand that many developers have similar needs and most instruction is based on what techniques and knowledge a majority of the community has. Nothing that Google says applies to all programmer's needs and there is a reason that the Application was not declared <code>Final</code>.</p> <p>Remember, there is a reason Android needs to be able to kill your components. And the primary reason is memory, not processing. By utilizing the Application as described above and developing the appropriate methods to persist the appropriate information, you can build stronger apps that are considerate to the System, the User, its sibling components AND other developers. Utilizing the information that everyone here has provided should give you some great guidance as to how and when to extend your <code>Application</code>.</p> <p>Hope this helps, FuzzicalLogic</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload