android - recovering activity state with getLastNonConfigurationInstance generates an unchecked cast warning -
in activity want use 'onretainnonconfigurationinstance' method store of data i've loaded in activity (no views). should speed loading , keep consistent state when orientation changes.
since return argument single object , want return 2 items i've come following solution:
@override public object onretainnonconfigurationinstance() { hashmap< string, object> data = new hashmap<string, object>(); data.put( "madapter", getexpandablelistadapter() ); data.put( "folderlist", folderlist ); return data; }
when collect data in oncreate method with:
hashmap<string, object> saveddata = ( hashmap< string, object> ) getlastnonconfigurationinstance();
i unchecked cast warning compiler. assume because compiler cannot determine if hashmap specified types going in object return getlastnonconfigurationinstance. cast objects in hashmap proper datatypes. question this:
is safe way pass multiple pieces of data activity oncreate when know data going returned in form of hashmap (because stored there)?
i think can suppress warning @suppresswarnings("unchecked") want sure code valid.
kind regards, ivo
why use hashmap instead of simple struct-like holding class when know comprehensive set of contents @ compile time?
also, beware passing holds reference context/activity between activity instances this. chances adapter you're passing holds context reference can layoutinflater inflate item views. have 2 negative effects:
- activities can big, , hanging on reference 1 has been destroyed keep garbage collector collecting it. (this sort of thing people mean when talk leaking contexts or activities.)
- contexts carry configuration info screen orientation , theme. if reuse old context inflating layouts after configuration change, you'll end using wrong configuration data. if you've used resource system provide different layouts portrait , landscape modes, example, won't work properly.
this why method called "non-configuration instance." correct return objects not influenced configuration. in case of adapter holds context, pass data adapter accesses rather adapter , create new adapter in new activity instance.
Comments
Post a Comment