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.