I have been in touch with Android for some time. I always thought that the entrance of the android program is the activity specified in the configuration file. I recently read an open-source project and found that the Android Application class is implemented, the real portal of the android program is the oncreate method of the application class. However, most developers do not need to override this class. Its Inheritance relationships are as follows: Java. Lang. Object Using Android. content. Context Using Android. content. contextwrapper Using Android. App. ApplicationThe android. App. Application class contains four public methods. Void onconfigurationchanged (configuration newconfig) Void oncreate () // here is the real entry point. Void onlowmemory () Void onterminate () The following is the test code: Two steps are required to use the application: 1. Rewrite the application class; 2. Configure the application in the configuration file. The sample code is as follows: MyApp class:
Package app. app; Import Android. App. Application; Import Android. content. res. configuration; Public class MyApp extends application { @ Override Public void onconfigurationchanged (configuration newconfig ){ Super. onconfigurationchanged (newconfig ); } @ Override Public void oncreate (){ Super. oncreate (); System. Out. println ("MyApp is called "); } @ Override Public void onlowmemory (){ Super. onlowmemory (); } @ Override Public void onterminate (){ Super. onterminate (); } } Configuration file: <Application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name" Android: Name = "app. App. MyApp"> ...... </Application>
Activity Class: Public void oncreate (bundle savedinstancestate ){ Super. oncreate (savedinstancestate ); Setcontentview (R. layout. Main ); System. Out. println ("mainactivity is called ");
} Result: MyApp is called mainactivity is called. NOTE 1: In Android, application is just a loose characterization concept, and there is not much substantive representation [which is significantly different from the MIDlet of j2_m2.] Application class, representing the context state of the application, is a concept of extreme weakening. Application is just a concept of spatial category, and application is the context description of components such as activity and service. Application is not the core concept of Android, but activity is the core concept of Android. Note 2: The myapplication class is used to place global variables and methods in some contexts. Original article: Real portal application of Android Program Http://www.pocketcn.com/forum-viewthread-tid-1565-fromuid-1477.html |