Activity Context vs Application Context

Brian Nicholson bnicholson at mozilla.com
Wed Feb 12 20:09:45 PST 2014


Which work are you referring to? The only bugs that I'm familiar with
are the dependencies of bug 891634, which change classes holding a
reference to the Activity Context to hold a reference to an
Application Context instead. These changes help prevent memory leaks,
and AFAICT, the classes in these bugs do not require a Context for any
UI.

On Wed, Feb 12, 2014 at 7:45 PM, Mark Finkle <mfinkle at mozilla.com> wrote:
> Does this mean our recent work to cleanse the code of Activity contexts and
> use Application contexts was misguided and could be causing problems, even
> crashes?
>
>
> ________________________________
>
> A worthy read: http://www.doubleencore.com/2013/06/context/
>
> When using Contexts, there are several different context types
> (Application, Activity, Service, etc. -- all mentioned in the link
> above). In the front-end code, the Application Context and Activity
> Context are the easiest to misuse.
>
> If you're doing anything UI-related, you *must* use the Activity
> Context. For example, this line showing a Toast looks innocuous
> enough:
> http://hg.mozilla.org/mozilla-central/file/879038dcacb7/mobile/android/base/home/HomeFragment.java#l157.
> But when you look at the Context being used, it's actually the
> Application Context. Red flag!
>
> On the other hand, when storing Context for long-lived objects
> (especially singletons), make sure you don't hold onto the Activity
> Context since that can result in memory leaks (An Activity can be
> recreated in the same Application instance). Better options are either
> a) pass the Activity Context to any methods requiring one, or b) if
> only the Application Context is needed, store the Application Context
> instead (e.g.,
> http://mxr.mozilla.org/mozilla-central/source/mobile/android/base/Tab.java#89).
> But be careful not to use this Context for anything UI-related for the
> reasons above.
>
> In short, all Contexts are not created equal, so we need to be careful
> how we use them.
>
> Brian
> _______________________________________________
> mobile-firefox-dev mailing list
> mobile-firefox-dev at mozilla.org
> https://mail.mozilla.org/listinfo/mobile-firefox-dev
>
>


More information about the mobile-firefox-dev mailing list