Things I wish I knew about Android before starting my first Android app

Activities lose state when the screen rotates, but Views don’t

All those member variables/fields you set while a user interacts with an activity are going to become null when the user rotates their screen. However, the views being managed by your activity will preserve themselves – whether they were visible, what text set in them, and some other properties. To preserve the info that will be lost to the screen rotation, store information by overriding public void onSaveInstanceState(Bundle outState), and restore it by pulling out out of the bundle passed into public void onCreate(Bundle instanceBundle).

Saving member variable mSomeValue

@Override
public void onSaveInstanceState(Bundle outState) {
	super.onSaveInstanceState(outState);
	outState.putSerializable("myKey", mSomeValue);
}

Restoring it next time the activity is created (such as after a rotation)

@Override
public void onCreate(Bundle instanceBundle) {
	super.onCreate(instanceBundle);
	/* more of your code here */
	mSomeValue = instanceBundle.getSerializable("myKey")
}

Network Requests

A bunch of people already figured out the best way to deal with queuing, retries, parsing, and keeping that stuff off the UI thread. Here’s a video on one library by a Google Play team member that was used in the official Google Play app – Volley. There are other libraries and structures that can help like ones described in this talk on REST requests

There’s something for making settings menus

It’s called PreferencesActivity and it will probably save you some time.

Try an Intent Service

The quickest way to get other operations out of the main thread? Cut synchronous code out of an activity or service, and paste it into an IntentService. Launch the IntentService with an Intent. Pass dependencies into the Intent Service inside the Intent’s extras.

XML Parsing Options

XPath is easy for the developer, but slow on the device. There are at least 2 other parsers that come with the Android and Java to look into.

Google Recommends the XmlPullParser. And this guy benchmarked these and other XML parsers.

NPM, Vagrant, Linux on Windows, shared directories and –no-bin-links

Error: UNKNOWN, unlink '/var/www/node_modules/grunt-lib-phantomjs/node_modules/phantomjs/README.md'

At its root, this is a permissions issue. Linux, running in virtual machine, has tried to make a directory in the Windows file system and been denied permission to do so. This line from the npm logs is talking about a failure to delete a file that doesn’t exist since it wasn’t created.

NPM to the rescue

Fortunately, NPM added a new option in 1.2.4, documented here:

The –no-bin-links argument will prevent npm from creating symlinks for any binaries the package might contain.

With this flag set, installing packages like grunt-contrib-phantomjs is now possible without other bizarre workarounds.

npm install grunt-contrib-phantomjs --no-bin-links

Beautiful!

W3 Total Cache makes blank pages after WordPress upgrade fix

Here’s the first fix I always try after W3 Total Cache starts serving blank white pages when I browse my site after updating WordPress. These instructions assume you know your way around the WordPress and W3 Total Cache admin areas.

Testing for blank pages

To test for the blank screen issue, be sure to view the site in Chrome’s Incognito mode or Firefox’s Private Browsing mode without logging into the admin area. Logging into the admin area creates cookies for storing your session, and W3 Total Cache will not serve cached pages to users who are logged in (unless you change it in W3 TC’s Page Cache configuration).

Debugging info

Also note that W3 TC will inject some extra comments into your page to give you debugging info. You can see it by viewing the source. This is a good way to check if Page Caching is enabled when you load a page. Here’s a sample:

!-- W3 Total Cache: Page cache debug info:
Engine:             disk: enhanced
Cache key:          kb.imakewebsites.ca/_index.html
Caching:            disabled
Reject reason:      User is logged in
Creation Time:      0.768s
Header info:
X-Pingback:          http://kb.imakewebsites.ca/xmlrpc.php
Expires:             Wed, 11 Jan 1984 05:00:00 GMT
Cache-Control:       no-cache, must-revalidate, max-age=0
Pragma:              no-cache
Content-Type:        text/html; charset=UTF-8
X-Powered-By:        W3 Total Cache/0.9.2.11
X-W3TC-Minify:       On
--

Environment Details

This bug appeared for me today moving from WordPress 3.5.1 to 3.6 with W3 Total Cache 0.9.2.11 on a DreamHost server (I’m not sure what versions of PHP and Linux and MySQL it uses). I’ve seen it happen on other hosts like an EC2 instance hosted on Amazon with Ubuntu when moving between other versions of WordPress as well.

A solution

In my experience, the blank pages are always caused by the Page Caching functionality (with Disk: Enhanced caching method).  Try following these steps to get your pages back:

  1. Save your W3 TC configuration. At the bottom of the plugin’s Dashboard you’ll find tools for exporting your configuration. Do it so you have a backup.
  2. Disable the W3 TC plugin
  3. Delete the W3 TC plugin
  4. Visit your site and verify that it works without W3 TC. If the white pages are still appearing, there might be another issue that I’m unfamiliar with.
  5. Install the W3 TC plugin.
  6. Enable the W3 TC plugin. You may find your configuration from the last install is still on the server and used automatically. If not, you have a backup from step 1!
  7. Visit your site in a Private or Incognito tab (remember the detail about cookies mentioned above). If WordPress and Page Caching is working again, you can stop following these instructions here
  8. Try disabling all W3 TC’s functionally from its Dashboard. The plugin will be active, but not doing anything helpful. Visit the site and check if it works now. If there are white pages or other issues, there might be something else wrong that I’m not familiar with.
  9. Turn on W3 TC Page Caching, and try the site again. You may need to clear the page cache as well.

If Page Caching works now that you’ve uninstalled, reinstalled, and reconfigured W3 TC, you can continue by turning on other parts of W3 TC and testing they work. If not, use the W3 Total Cache Support boards to find a solution to your problem.