I have a very simple code which displays a Toast message upon a Button click
public class MainActivity extends Activity {
Button clickMe;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
clickMe = (Button) findViewById(R.id.button1);
clickMe.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast msg = Toast.makeText(getApplicationContext(), "Hello World !", Toast.LENGTH_LONG);
msg.show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
I was wondering how I'm able to get reference to the Context object simply invoking
getApplicationContext()
inside Toasts' makeText method since I never instantiated Context (which I can't anyway since it's abstract) nor it's subclasses.
Can anyone please explain me when (or where) the reference was created and how come it's available in my class MainActivity?
A simple example with few classes to achieve the same will be highly appreciable.
thanks in advance!
0 comments:
Post a Comment