Context inheritance is as follows:

  • The Application inherits from ContextWraper and the Activity inherits from ContextThemeWrapper (ContextThemeWrapper is a subclass of ContextWraper).

Context is shown as follows:

In the figure above, you can see that there are numbers in the upper right corner of No, where the meanings of numbers are as follows:

  • 1: Starting an Activity is ok in these classes, but you need to create a new task. Generally not recommended.

  • 2: The layout inflate is legal in these classes, but the system default theme style is used, and some styles may not be used if you customize them.

  • 3: allows when receiver is null. In 4.2 and above, it is used to obtain the current value of sticky broadcast.

The specific differences are as follows:

  • The life cycle of the Application is much longer than that of the Activity. For objects with a long life cycle, the Application is generally used as the context to avoid memory leakage caused by improper holding of the Activity.
  • Application cannot showDialog. ShowDialog must have an Activity Context.
  • When applying startActivity, a new Task must be created.
  • The Application uses the default theme directly when layoutsynchronized, which may not be the same as the current theme, causing some of our Settings to fail.

Total number of Contexts in Android = Number of Activities + Number of Services + 1 (Application)Copy the code