I was using static makeText()
function of Toast
class in android studio.Now,as makeText()
function requires an object of Context
class ,what we can do is create an object of Context class by below given code.
Context context = getApplicationContext();
and use this context
object as the parameter in makeText()
function.That's OK.
But I was reading a book which used the makeText()
function along with show()
as below given code.
Toast.makeText(this,"Can you see me",Toast.LENGTH_SHORT).show();
I read on this page that this
keyword can be used to get a Context
.
So,my question is why using this
keyword as a parameter of a static function of Toast class refers/gives/returns specifically a Context
when MyActivity
(from below code) class extends directly/indirectly AppcompatActivity
,FragmentActivity
,Activity
,ContextThemeWrapper
,ContextWrapper
classes or why not 'this' keyword refers to an object of immediate class 'MyActivity' ?
public class MyActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
Toast.makeText(this,"Bhai app chalu ho gai",Toast.LENGTH_SHORT).show();
Log.i("info","Done creating the app");
}
}