Home java Why do I need context in Android?

Why do I need context in Android?

Author

Date

Category

stumbled upon the code, when creating an intent, the CONTEXT object is transmitted. Why is it needed, I do not understand? If you can, as simple as possible.


Answer 1, Authority 100%

context is an object that provides access to the basic functions of the application: access to resources, to the file system, challenge activity, etc. Activity is a subclass context , so in the code we can use it as Name Nativity.This (eg MainActivity.this ), or the shortened entry This . Classes Service , Application et al. Also work with context.

Access to the context can be obtained in different ways. There are methods such as getApplicationContext () , getContext () , getbasecontext () or this , which was mentioned above If used in activity.

At first, it is not necessary to understand why it is needed. It is enough to remember the methods that allow you to get a context and use them if necessary, when some method or constructor will require the CONTEX object in its parameters.

In turn, Context has its own methods that allow access to resources and other objects.

getassets ()
GetResources ()
getPackagemanager ()
getstring ()
GetSharedprefsfile ()

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions