Getting started with Android (5): understand the Context that Activity logging has to say

Source: Internet
Author: User

During Android development, you can always meet the Context class or its instances. Context instances are often used to provide reference for "Applications. For example, in the following code snippet, the first parameter of the Toast class accepts a Context object:


[Java]
@ Override
Protected Dialog onCreateDialog (int id ){
Switch (id ){
Case 0:
 
Builder builder = new AlertDialog. Builder (this );
Builder. setIcon (R. drawable. ic_launcher );
Builder. setTitle ("This is a dialog with some simple text ...");
Builder. setPositiveButton ("OK ",
New DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog,
Int whichButton ){
Toast. makeText (getBaseContext (), "OK clicked! ",
Toast. LENGTH_SHORT). show ();
}
});
 
Builder. setNegativeButton ("Cancel ",
New DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog,
Int whichButton ){
Toast. makeText (getBaseContext (), "Cancel clicked! ",
Toast. LENGTH_SHORT). show ();
}
});
 
Builder. setMultiChoiceItems (items, itemsChecked,
New DialogInterface. OnMultiChoiceClickListener (){
Public void onClick (DialogInterface dialog, int which,
Boolean isChecked ){
Toast. makeText (
GetBaseContext (),
Items [which]
+ (IsChecked? "Checked! "
: "Unchecked! "),
Toast. LENGTH_SHORT). show ();
}
});
Return builder. create ();
 
}
Return null;
}
@ Override
Protected Dialog onCreateDialog (int id ){
Switch (id ){
Case 0:

Builder builder = new AlertDialog. Builder (this );
Builder. setIcon (R. drawable. ic_launcher );
Builder. setTitle ("This is a dialog with some simple text ...");
Builder. setPositiveButton ("OK ",
New DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog,
Int whichButton ){
Toast. makeText (getBaseContext (), "OK clicked! ",
Toast. LENGTH_SHORT). show ();
}
});

Builder. setNegativeButton ("Cancel ",
New DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog,
Int whichButton ){
Toast. makeText (getBaseContext (), "Cancel clicked! ",
Toast. LENGTH_SHORT). show ();
}
});

Builder. setMultiChoiceItems (items, itemsChecked,
New DialogInterface. OnMultiChoiceClickListener (){
Public void onClick (DialogInterface dialog, int which,
Boolean isChecked ){
Toast. makeText (
GetBaseContext (),
Items [which]
+ (IsChecked? "Checked! "
: "Unchecked! "),
Toast. LENGTH_SHORT). show ();
}
});
Return builder. create ();

}
Return null;
}
However, the Toast class is not directly used in the Activity and is used in the AlertDialog class. Therefore, you must use the getBaseContext () method to obtain an instance of the Context class.

When a view is dynamically created in an Activity, Context is also displayed. For example, if you want to dynamically create a TextView with excellent encoding:


[Java]
TextView TV = new TextView (this );
TextView TV = new TextView (this); the constructor of TextView accepts a Context object. Because the Activity class is a subclass of the Context class, you can use the this keyword to replace the Conext object.

 

Tip:

Using this to dynamically create views, such as TextView and Button, poses a potential risk-Memory leakage. Therefore, replace this with the getApplicationContext () method whenever possible.

 

From horsttnann
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.