A bug in (and a fix for) the way FragmentStatePagerAdapter handles fragment restoration

Ever used a FragmentStatePagerAdapter? We’re using one at work for our ticket purchasing wizard. The user enters the wizard, and can progress to the next page once they’ve completed the current one. We control this by manipulating the stack of pages and notifying the adapter that the data has changed when a new page is available.

Unfortunately, when changing pages that have already been loaded, there’s an unexpected bug. Specifically, when you load a page, remove it and then insert a new one in its place, the next time the fragment at that index is loaded, it receives the savedInstanceState bundle for the old fragment.

Read on →

20 Things, Week 20

Week 20 of my 20 Things in 20 Weeks project. I’ve finally made it. So, how did I do?

Read on →

20 Things, Week 19

Broke my Github streak this week, at 31 days - in fact, I didn’t get any programming done this week. My partner and I left Auckland on Wednesday afternoon and didn’t arrive home until late Sunday evening. We travelled around the Coromandel peninsula and spent time climbing mountains, visiting beaches, doing touristy things - and we got engaged! I’d say that that’s a pretty good excuse for missing a week.

20 Things, Week 18

Last week I wrote a Sudoku solver script in Python. That was fun and all, but as my girlfriend pointed out - it’s not really usable for, er, well, most people. So this week I decided to make it a bit more versatile and build it into a service, with a website to go with it. It’s currently up on Github and once I’ve added some polish to the website (heh, it’s pretty awful right now) I’ll get it up on App Engine.

Read on →

20 Things, Week 17

This week I finally got around to doing something I’ve wanted to do for a while; I wrote a Sudoku Solver in Python.

Unfortunately, it doesn’t work. At all. In fact, the more I look into it, the worse I fear it is. I’ve attempted this before, but I struck a minor obstacle with some Python syntax and did something else instead; this time, I’ve got the entire algorithm worked out and developed. There’s just some fairly significant logic bug(s) that’re causing problems. Re-reading the code, there’s also quite a bit of that which could do with a rewrite.

No matter! I’m pretty happy I got the majority of the work done, and I’ll keep working on it - a bit of refactoring should hopefully make the logical errors a bit more obvious.

Edit (Jan 21 2014): Fixed.

20 Things, Week 16

I had my first open-source pull request merged this week, into Dogr.io.

Read on →

20 Things, Week 15

I checked in some code every day this week. That being said, I don’t really have a ‘Thing’ that I’ve done. Turns out that I’m running out of ideas for small, week-long projects. My plan to do some small things and even a bigger project over my 2 week break didn’t work out so well - Christmas takes up a lot of time, and there was plenty of other things to do in my time off, too.

So while I haven’t done any one thing worthy of note, some of the stuff I have done (these are mostly quite trivial):

  • Implemented PullToRefresh library
  • Implemented the Crouton library for toasts
  • Decided that loading 1000+ items with ORMLite to back a ListView was not a good idea and actually got around to writing the plumbing code to use a Cursor
  • Added a check for ‘last checked date’ so we now query for the smallest number of quakes

20 Things, Weeks 13 and 14

You may have noticed (heh, who am I kidding - no one is sitting around waiting for these captivating posts) that I missed week 13 of my 20 Things, 20 Weeks project. I could make excuses about it being Christmas, and being super busy, but that’d be a lie. I was probably playing Hearthstone.

Read on →

20 Things Week 12

Actually managed to get some code written this week, though it’s still pretty rough - I’ve got a basic widget working for What’s Shaking, NZ? for Android. Exciting stuff; that’ll be polished up and good to go next week. It was actually really easy to do, as I can update it directly from my existing background service (which polls the NZ Geonet API).

Also wrote a blog post on a common Java encoding issue - basically, when you’ve got a UTF-8 file with a Byte Order Mark at the start, things that parse that can freak out if they’re not expecting it. Easiest way to avoid it is to get rid of the BOM altogether.

Broken JSONObject creation from a UTF-8 input String

12-16 12:01:40.446: W/System.err(3873): org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONObject

Faced this issue, again, at work today. We have a build system with build variants for different customers. To add a new customer, we just create a new folder, add the images and add a JSON config file to suit the new customers settings. We read from that file and into a JSON string (and then into a JSON object) something like this:

1
2
3
InputStream inStream = context.getResources().openRawResource(R.raw.local_config);
String json = IOUtils.toString(inStream);
JSONObject jsonObject = new JSONObject(json);

Sometimes, new customers speak a language other than English, and we have to save non-ASCII characters. In this case, the file gets saved as UTF-8. Testing this isn’t a problem on my devices (Galaxy S2/Nexus 7) - but my tester has twice come back to me now and said that it doesn’t work on our 2.3 device.

Read on →