1. onbuildheaders (list <preferenceactivity. header>) in the previous blog (when to use preference headers), we described the role and use cases of preference headers. onbuildheaders is used to create and initialize headers in preferenceactivity.
After creating headers, We can reference the headers instance in the program, but pay attention to the creation time to avoid errors caused by referencing before the creation.
2. Call time
- By analyzing the source code of preferenceactivity, we can see that the headers creation time is called in the oncreate () process, as shown below:
@ Override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (COM. android. internal. r. layout. preference_list_content); mlistfooter = (framelayout) findviewbyid (COM. android. internal. r. id. list_footer); mprefscontainer = (viewgroup) findviewbyid (COM. android. internal. r. id. prefs_frame); Boolean hidingheaders = onishidingheaders (); msingdomainane = hidinghea Ders |! Onismultipane (); string initialfragment = getintent (). getstringextra (extra_show_fragment); bundle initialarguments = getintent (). getbundleextra (extra_show_fragment_arguments); int initialtitle = getintent (). getintextra (extra_show_fragment_title, 0); int initial0000title = getintent (). getintextra (extra_show_fragment_short_title, 0); If (savedinstancestate! = NULL) {// We are restarting from a previous saved state; used that to // initialize, instead of starting fresh. arraylist
- During oncreate () implementation in the inherited preferenceactivity, we must make it clear that the initialized headers can be referenced only after super. oncreate () is called. Otherwise, an error will occur.