Android (Java) Learning Note 223: Context-sensitive

Source: Internet
Author: User

1. Two below:

(1) Context of the Activity.this interface

(2) Getapplicationcontext () the context of the entire application

2. Usually the two are interoperable, but the following dialog creates a scenario that can only be used with Activity.this, as follows:

1  PackageCom.itheima.context;2 3 Importandroid.app.Activity;4 ImportAndroid.app.AlertDialog;5 ImportAndroid.app.AlertDialog.Builder;6 ImportAndroid.os.Bundle;7 ImportAndroid.view.View;8 ImportAndroid.widget.TextView;9 Ten  Public classMainactivityextendsActivity { One  A @Override -     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); the Setcontentview (r.layout.activity_main); -        (1)TextView TV = new TextView (mainactivity.this);//cannot use Getapplicationcontext () -     } -      +      Public voidClick (View view) { -        (2) Alertdialog.builder builder = new Builder (this); +Builder.settitle ("Warning"); ABuilder.setmessage ("The dialog box is out.")); atBuilder.setpositivebutton ("OK",NULL); - builder.show (); -     } -  -}

You cannot use Getapplicationcontext () in the above (1) and (2):

(1) TextView TV = new TextView (mainactivity.this); TV is dependent on activity (the interface exists), activity is destroyed, and TV is destroyed .

If you use TextView TV = new TextView (getapplicationcontext ()), the activity may be destroyed, But the entire application has not been destroyed, so the TV will become a null pointer, causing memory leaks.

(2) Alertdialog.builder builder = new Builder (mainactivity.this);

The same dialog box is created, and the dialog box is also dependent on activity, if you use alertdialog.builder builder = new Builder (Getapplicationcontext () );

This getapplicationcontext () is the parent of the activity, and the parent class may be inherited by many subclasses, so that the dialog cannot be positioned on which activity to display and will error .

The display of both TV and builder in (1) and (2) must be bound to a specific activity interface

Android (Java) Learning Note 223: Context-sensitive

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.