Skip to content

Instantly share code, notes, and snippets.

@pokk
Last active February 21, 2017 04:13
Show Gist options
  • Save pokk/5347797cb87cd5b18199abd60e7e8b1b to your computer and use it in GitHub Desktop.
Save pokk/5347797cb87cd5b18199abd60e7e8b1b to your computer and use it in GitHub Desktop.
Context using scenarios in the common.

[TOC]

Introduction

How to use Context correctly in android.

How many contexts in an application

As following we understood, subclasses of Context are Activity, Service, Application, ...etc.
So we can know there are (number of activities + number of services + 1) of context numbers in an application.

  1. Activity (subclass of context)
  2. Service (subclass of context)
  3. Broadcast Receiver (by pass context)
  4. Content Provider (by pass context)

Scenarios

Application Activity Service ContentProvider BroadcastReceiver
Show a Dialog N Y N N N
Start an Activity N1 Y N1 N1 N1
Layout Inflation N2 Y N2 N2 N2
Start a Service Y Y Y Y Y
Bind to a Service Y Y Y Y Y
Send a Broadcast Y Y Y Y Y
Register BroadcastReceiver Y Y Y Y N3
Load Resource Values Y Y Y Y Y

NOTE:
N1 : Actually they can start an activity, but they need to create a new task for it. It's not recommended.
N2 : It's legal to use layout inflate, but it will be system default theme. If you customed your theme individually, it's not recommended. Normally, We should use the context of Activity to handle the operators of all of related with UI.
N3 : ??

There is a context users can use it in ContentProvider and BroadcastReceiver.

How to get Context

  1. View.getContext(): Get a context object of current View. It's Activity object normally.
  2. Activity.getApplicationContext(): Get a context object of current Activity in this application. It's high priprity to be considered for using.
  3. ContextWrapper.getBaseContext(): It's not recommeded.
  4. Activity.this: Get a instance of current Activity. It's as a context to used in UI component. ★ ApplicationContext can use in TOAST.

getApplication() vs getApplicationContext()

APPLICATION IS A CONTEXT

Following a test, getApplication() and getApplicationContext() are the same address(they are equal). Why do they design two duplicated methods to get the same thing in Android? The reason is as below.

  1. getApplication(): It only can be obtained in Activity and Service.
  2. getApplicationContext(): If you want to obtain a Application instance in BroadcastReceiver. This way can help you to obtain it.

The good way to use

Context causes memory leak or application crash, there are some reasons, one of them is when Context will be destroied, some objects keep the Context.

  1. Using Context of the application in normal situations and the object with long lifecycle as possible as we can.
  2. Don't let an object hold a context reference of Activity.
  3. Try not to use non-static inner classes in Activity, because non-static inner classes implicitly hold references to external class instances. If you use static inner classes, hold external instance references as weak references.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment