What is Context?
Context: It is nothing but current state of object or application. Typically you call it to get data regarding another part of your program (activity & package/application). You can get the context by writing below code.
There are two type of context
- Application Context
- Activity Context
Application context is related to the application’s life cycle and it will be same whole application.When ever we use toast messages we can use application context or activity context because toast can raised anywhere in application
Activity context is attached to Activity life cycle and can be destroyed once activity destroyed.If we want to start new activity we must use activity context
the above line is an Activity which is obviously a Context.. this is used when you create some Alert Dialogs... At some places its compulsory that you use Activity Context… Intent i =new Intent (MainActivity.this,Second.class); startActivity(i);
It will return the context of entire application.We will use getApplicationContext if we need a context tied to the lifecycle of entire application not just current activity. Toast.makeText(getapplicationContext(),”this is toast”,Toast.Length_Long).show();
It will return the context of the current view.Mostly we use this inside of the fragments.
If you require access to a Context from inside another context, you use a ContextWrapper. The Context alluded to from inside of the fragments
If you require access to a Context from inside another context, you use a ContextWrapper. The Context alluded to from inside that ContextWrapper is accessed via getBaseContext().
kosmik provides Android training in Hyderabad, we are providing lab facilities with complete real time training & 100% job assistance will be there