importance of Context in android
Possible Duplicate:
What is Context in Android?
i read context in android current state of the application/object
Intent intent=new Intent(this,SecondaryActivity.class);
startActivity(intent);
in the place of this
we can use getApplicationContext()
but in an
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Hai",Toast.LENGTH_LONG).show();
}
we can't use this
here i mean in getApplicationContext()
why this also refers to the current object ,hence am confused about this
and context
help me to study this,an reference share me..
When you pass this
to method, you mean that this
reference is an instance of Context
, so if you are in Activity you can pass this instead of Context. But when you are in anonymous class:
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(){
//here this is a reference to OnClickListener instance
}
To pass Context
in onClick
method you can write
MyActivity.this
Activity is a sublcass of context so all Activity
objects are also a Context
:
android.content.Context
↳ android.content.ContextWrapper
↳ android.view.ContextThemeWrapper
↳ android.app.Activity
So if you can't use this
than it means this
is not an instance of an Context
class or its subclass.
上一篇: Android什么是上下文?
下一篇: 在android中的上下文的重要性