Read The Fucking Source Code

The introduction

  • Context: The most familiar stranger.
  • The all-powerful man: Start Activity, start service, register/send broadcast, get ContentResolver, get classloader, get resource, operate database.

Android R — API 30

Overall overview

1. Inheritance diagram

Overall description

【 Context 】 : The ability to interact with others (the all-around guy mentioned above).

  • Context: The Context capability is defined collectively.
  • ContextWrapper: Base class of the core component that is associated with ContextImpl, ContextImpl, and ContextImpl.
  • ContextImpl: Context-capable execution proxy (real execution).
  • ContextThemeWrapper: Provides theme capability, specifically for activities.
  • DecorContext: Created for decorViews, with theme capabilities, for better classification and decoupling (versus activities).
  • ReceiverRestrictedWrapper: born to BroadcastReceiver, castration off binding service ability.

2. Associate the component with the Context

2.1 Application

2.2 the Activity

2.3 the Service

2.4 BroadcastReceiver

2.5 ContentProvider

3. Core method description

3.1 Basic class core method description

3.2 Application component core method description

4. Summary

4.1 Unique Activity launch

The Activity Context can be startActivity directly, and other contexts need to be FLAG_ACTIVITY_NEW_TASK flags to use. Why is that? Because of the specificity of the Activity’s UI view management, the Activity takes over the functionality that would otherwise be implemented by the ContextImpl proxy class. So the same method is handled differently on the Activity side and ContextImpl.

4.2 Unique Activity dolls

The Activity getBaseContext is ContextThemeWrapper. If we want to use the ContextImpl method in our Activity, for example: So getBaseContext().startActivity, the call level is:

  1. Get the Context reference:getBaseContext– > ContextThemeWrapper;
  2. Inheritance relationship, calling the parent directly:ContextThemeWrapper.startActivity -> ContextWrapper.startActivity;
  3. Calling ContextImpl (mBase) proxy:ContextWrapper.startActivity -> mbase.startActivity.

Compare Application getBaseContext() with startActivity.

  1. Get the Context reference:getBaseContext– > ContextImpl;
  2. Calling ContextImplstartActivity.

4.3 Emasculated BroadcastReceiver

The Context of the Receiver cannot perform bindService operations. Otherwise, throw an exception directly (source code will not be put). The Context of the Receiver performs registerReceiver operation handling. This function is allowed only when receiver == null is used to obtain sticky broadcasts.

4.4 getApplicationContext differences

  • Application:getApplicationContext()Application;
  • The Activity/Service:getApplication()getApplicationContext()Is Application;
  • BroadcastReceiver: used during onReceivegetBaseContext().getApplicationContext()Access to the Application;
  • Use ContentProvider:getBaseContext().getApplicationContext()Get the Application (which may of course be empty: the Provider does not create an Application object during initialization, multiple APKs are running in the same process, and the second Apk is initialized through the Provider, so the Context is empty).

Xiaobian extension links

Android Basic Components Family Bucket