More likely, that switch statement will crash with a VerifyError on older versions of Android, since apply() will not exist. Even if the statement is not executed, the virtual machine will still try to find it when it loads the class containing this code, and that will trigger the VerifyError.
Another approach is to use an abstract class with a pair of concrete implementations, one for commit() and one for apply(), and a singleton to provide the right concrete implementation based upon API level. Here is a sample project demonstrating using this technique to support front-facing cameras on Android 2.3 or the default camera on older versions of Android: https://github.com/commonsguy/cw-advandroid/tree/master/Camera/Picture On Tue, Jan 18, 2011 at 9:49 AM, davemac <[email protected]> wrote: > Yikes! Two thoughts here. One, that switch statement is a bad idea > since the behavior in Honeycomb (and Ice Cream Sandwich, etc) will > revert back to a commit(), unless you're incredibly diligent about > updating your code in a timely fashion. The second and scarier thought > is that many times we're told that reflection can be used to figure > out if something is available or not. According to Dianne, reflection > could tell you that it's there but we still shouldn't use it because > it could be not-yet-ready-for-prime-time, even though it got released. > Important safety tip everyone! > > - dave > > On Dec 16 2010, 5:47 pm, "Raymond C. Rodgers" > <[email protected]> wrote: >> I think what Dianne was suggesting might be something like doing a >> switch on the user's Android version... This isn't necessarily working >> code but should give you an idea: >> >> Edit e = prefs.Editor(); >> e.put("Whatever","Data"); >> switch(android.os.Build.VERSION) >> { >> case Build.GINGERBREAD: >> e.apply(); >> break; >> default: >> e.commit(); >> >> } >> >> It's simple enough unless you have your preference writing routines >> scattered all over the place, in which case you could simply write a >> function to write preferences. >> >> Raymond >> On 12/14/2010 12:14 AM, shareem sharif wrote: >> >> > ture but no >> >> > On Mon, Dec 13, 2010 at 8:28 PM, Dianne Hackborn <[email protected] >> > <mailto:[email protected]>> wrote: >> >> > You should do this based on API level. If an API is not >> > documented as available prior to a particular version, there are >> > no guarantees what any existing method would do in the earlier >> > versions. For example, not infrequently a new API is being >> > developed but hidden from a particular release because it is not >> > ready yet due to being buggy or other reasons that could cause you >> > trouble. (I have no idea if that is the case here, but best to be >> > safe.) >> >> > On Mon, Dec 13, 2010 at 7:59 PM, Zsolt Vasvari <[email protected] >> > <mailto:[email protected]>> wrote: >> >> > As per theStrictModearticle ( >> > >> > http://android-developers.blogspot.com/2010/12/new-gingerbread-api-st... >> > ), I devided to switch over using apply() instead of commit() >> > for my >> > SharedPreferences. >> >> > So I wrote my nice conditional logic based on the API level and to >> > test to see what happens, I ran it on my 2.2 Nexus One, fully >> > expecting a VerifyError because of the missing API. To my >> > surprise, I >> > didn't get that error. So I fired up the emulator and >> > confirmed that >> > all the day down to Level 7, the supposedly new API does not >> > trigger a >> > VerifyError. It does level 4 or below as expected. >> >> > What's going on here? Has the apply() method really been >> > there since >> > Level 7 undocumented? If so, is it safe for me to check if the >> > current API level >= 7 and use apply()? >> >> > -- >> > You received this message because you are subscribed to the Google >> > Groups "Android Developers" group. >> > To post to this group, send email to >> > [email protected] >> > <mailto:[email protected]> >> > To unsubscribe from this group, send email to >> > [email protected] >> > <mailto:android-developers%[email protected]> >> > For more options, visit this group at >> > http://groups.google.com/group/android-developers?hl=en >> >> > -- >> > Dianne Hackborn >> > Android framework engineer >> > [email protected] <mailto:[email protected]> >> >> > Note: please don't send private questions to me, as I don't have >> > time to provide private support, and so won't reply to such >> > e-mails. All such questions should be posted on public forums, >> > where I and others can see and answer them. >> >> > -- >> > You received this message because you are subscribed to the Google >> > Groups "Android Developers" group. >> > To post to this group, send email to >> > [email protected] >> > <mailto:[email protected]> >> > To unsubscribe from this group, send email to >> > [email protected] >> > <mailto:android-developers%[email protected]> >> > For more options, visit this group at >> > http://groups.google.com/group/android-developers?hl=en >> >> > -- >> > You received this message because you are subscribed to the Google >> > Groups "Android Developers" group. >> > To post to this group, send email to [email protected] >> > To unsubscribe from this group, send email to >> > [email protected] >> > For more options, visit this group at >> >http://groups.google.com/group/android-developers?hl=en > > -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en > -- Mark Murphy (a Commons Guy) http://commonsware.com | http://github.com/commonsguy http://commonsware.com/blog | http://twitter.com/commonsguy _The Busy Coder's Guide to *Advanced* Android Development_ Version 1.9 Available! -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

