Context-related content is often encountered in Android
A brief discussion of the context: in the statement alertdialog.builder Builder = new Alertdialog.builder (this); , the parameter required to be passed is a context, where we are passing in this, then what does this one refer to? Here this refers to the activity.this, which is the activity of this statement, which is the context of this activity. It's not right to have a lot of friends on the Internet passing in This.getapplicationcontext (). The Alertdialog object is dependent on a view, and the view is corresponding to an activity. So, here comes a life cycle problem, This.getapplicationcontext () takes the context,activity.this of this application is the context of the Activity, the life cycle of the two is different , the life cycle of the former is the entire application, and the latter's life cycle is only the activity in which it resides. And Alertdialog should belong to an activity, when the activity is destroyed it is destroyed, no longer exist; Into This.getapplicationcontext (), it means that its life cycle is the entire application, which clearly exceeds its life cycle. So, here we can only use the activity's this.
Error occurred with new Alertdialog.builder (Getapplicationcontext ()):
E/androidruntime (5844): android.view.windowmanager$badtokenexception:unable to add window--token null was not for a app Lication
Then looked up:
Getapplicationcontext () life cycle is the whole application, application destroy it to destroy Activity.this context belongs to activity, activity destroys he destroys
Activity.this to return an activity, getapplicationcontext () does not necessarily return an activity
Getapplicationcontext () Returns the context of the application, the life cycle is the entire application, and the application destroys it before it destroys
The context of the Activity.this returns the current activity, belonging to the activity, destroying the activity and destroying it.
Getbasecontext () returns the context specified by the constructor or Setbasecontext () setting
Turn Android to learn--context and Getapplicationcontext ()