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