Why does Android use multiple contexts and each is different?

I know that in android there are many contexts like

  • Application
  • Activity
  • Service
  • ContentProvider
  • BroadcastReceiver

So when I create a new TextView , I have to pass context in a constructor like this

 TextView textView = new TextView(this); 

I know this is necessary, but why not just create and android process the context for me?

+6
source share
3 answers

I like to think about context visually, since I am an active user of Fragments , so most of the time when I pass context or inherit an instance of context , it will usually be from Activity .

Interface to global information about the application environment. This is an abstract class, the implementation of which is provided by the Android system. It allows you to access resources and classes of applications, as well as forwarding for operations at the application level, such as launching, broadcasting and receiving intentions, etc.

as described in Android Developers . It basically gives you help that you can perform β€œapplication-level chimes,” so let's talk about this in detail in your case.

I know this is necessary, but why not just create and android process the context for me?

A new instance of your TextView created without an instance of context . Thus, it will look something like this: TextView tv = new TextView(); will confuse Android as to where the TextView is being created. How do they know if this is an application level or activity level? What characteristics would a TextView before creating an instance? when you go to the TextView constructor, you will notice how it needs important information before it generates a new instance of the specified TextView . For example: final Resources.Theme theme = context.getTheme(); - this is only one line where they collect information through the context instance, now they know where the information comes from, then they can apply the corresponding topic.

How does Android know where you got this class from, and what topic would you like to apply to it if you didn't tell them?

Finally, to answer your question: β€œWhy not just create an android to handle the context for me?” this IS tool for Android that you need, but it must know where you are from and where you are in the life cycle.

Edit: added from comments.

why not this topic was initialized in the context of the application and used in the text field code without my interference

because again it comes down to where you want this widget to be based. So let me say that I want a TextView inside my Activity , but we call the application-level context, the theme that will be applied automatically will be @style/apptheme . But if I want the TextView to TextView the same style rules in the current Activity , instead of manually changing the theme for each widget I want to create (I'm a developer), android processes it for you, no matter where you are in application. this simplifies styles by creating new instances of TextView simple, etc. etc.

you know that I remember the script from the .NET platform, when I create a new button in the form and without any thing passed to the constructor, it automatically inherits its theme for the parent form .. you think that .NET design is better on this or what?

Unfortunately, I have no experience talking with .NET, but I think about the ever-changing state of activities, services and recipients while using an application that could be closed and opened at any time. This is a nice feature that lets Android know where you are and what you create.

+2
source

I think all about the Context life cycle. For example, an Activity can be completed and garbage collected, making it Context equal to zero. It can also work the other way around. A link in the context of an Activity can cause a memory leak in some cases, and an Activity will never be garbage collected.

Mark this answer as well

+1
source

Context is a way of managing memory. This is probably the most used element in Android apps ... it may also be the most incorrect.

The general actions that you can safely perform with a given Context object depend on where it came from originally.

Below is a table of common places in which the application will get context, and usage for each case mentioned: Read this, I hope you find it useful https://possiblemobile.com/2013/06/context/

0
source

Source: https://habr.com/ru/post/1013383/


All Articles