[JAVA] [Android] What is Context? ??


I researched and learned about Context in my own way, so I will leave it in the article.

As a trigger When I was developing Android and passed Context as a method argument without thinking about anything, I received such a review from a lead engineer.

Be careful of overdose of Context. Before I knew it, I couldn't get out of it and eventually I couldn't touch it. If you add Context to the argument, you should be aware that you will lose and do not add Context to the argument.

So I wondered "What is Context?" And researched various things.

What is Context in the first place? ??

Meaningful Context in Japanese

(Sentence) context, context, context, background, situation, environment

And so on

Context in programming

Excerpt from this site http://e-words.jp/w/%E3%82%B3%E3%83%B3%E3%83%86%E3%82%AD%E3%82%B9%E3%83%88.html

Even in the field of programming, the same code description and program elements may behave differently or be subject to different restrictions depending on their position in the program and the internal state when they are executed. It is sometimes called a context.

Context in Android development

Class definition and inheritance relationship

--Context is defined in an abstract class --Activity superclass (Activity inherits Context class) --Inheritance: Context ContextWrapper ContextThemeWrapper Activity --Application superclass (Application inherits from Context class) --Inheritance: Context ContextWrapper Application

By the way, getApplicationContext () is defined in ContextWrapper class

When to appear and use

--When Dialog is generated --When using Intent --When accessing application resources (eg getString ())

And so on ... Anyway

Apparently there are two types of Contexts (there are others, but they are omitted this time).

Activity Context

--Depends on the activity life cycle. Therefore, when Activity is destroyed (such as when the screen is rotated), Context is also destroyed. --The instance doesn't seem to be a singleton.

Acquisition method

Difference between getContext () and requireContext ()

--getContext (): Returns a nullable Context (treated as Context? In Kotlin) as a value --requireContext (): Returns a non-null Context as a value

Reference material

Application Context

--It is application related and does not depend on the activity life cycle. --It will not be destroyed or modified while the application is alive as a singleton.

Acquisition method

Java: getApplicationContext() Kotlin: applicationContext

How is it desirable to use them properly?

In conclusion, it has become an abstract one, "** Implement after considering the life cycle and the risk of memory leaks **".

However, basically, it seems better to use ActivityContext as much as possible in consideration of the risk of memory leak. I referred to multiple articles, but there were various opinions.

Notes on handling Context about memory leak

--If the lifetime of the Context is longer than the lifetime of the receiving object: preferred implementation --If the life of the receiving object is longer than the life of the Context: There is a risk of memory leak

So what if you come across Context in the future?

Until now, I used Context appropriately without thinking about anything, but in this situation, "Which is better to use, ActivityContext or ApplicationContext?” I will implement it while thinking. We will deepen our understanding in that.

Special Thanks (reference material)


If you have any mistakes or suggestions, please feel free to comment!

Finally, in conclusion, Context is difficult ... Thank you very much.

Recommended Posts

[Android] What is Context? ??
What is Cubby
What is Docker?
What is null? ]
What is Keycloak
What is maven?
What is Jackson?
What is Docker
What is self
What is Jenkins
What is ArgumentMatcher?
What is params
What is SLF4J?
What is Facade? ??
What is Java <>?
What is Gradle?
What is POJO
What is Java
What is centOS
What is RubyGem?
What is programming?
What is before_action?
What is Docker
What is Byte?
What is Tomcat
Understand what Dagger2's Android support is doing
What is Maven Assembly?
What is `docker-compose up`?
What is a constructor?
What is vue cli
What is an interface?
What is Ruby's self?
What is hard coding?
What is a stream
What is Ruby's attr_accessor?
What is Java Encapsulation?
What is permission denied?
What is instance control?
What is an initializer?
What is Spring Tools 4
What is an operator?
What is object orientation?
What is Guava's @VisibleForTesting?
What is Java technology?
What is Java API-java
What is @ (instance variable)?
What is Gradle's Artifact?
What is JPA Auditing?
[Swift] What is dismiss?
What is a Servlet?
What is web development?
[Java] What is JavaBeans?
[Java] What is ArrayList?
[Ruby] What is true?
What is object-oriented after all?
What is HttpSession session = request.getSession ();
What is docker run -it?
What is Java Assertion? Summary.
[Memorandum] What is an error?
What is DI (Dependency Injection)
What is object-oriented programming? ~ Beginners ~