Edit Mode And why using a Contextual ActionBar is a bad idea.

Recently I've seen a lot of designers using an edit mode for user data entered into an application, usually some form of profile.

They want this section of the app to be guarded by an icon (usually a pencil) in the ActionBar, which when the user clicks, turns all of the TextView on the page turn into EditText.

"How do we handle this state?" I hear you ask, well for a lot of people the answer seems to be a Contextual ActionBar. Sure it gives you the nice done and save icon the designer wants without having to mess with the ActionBar, but, and this is a big but. It will crash.

You might not notice it yourself, you may not get many crash reports about it, but it will happen to somebody. I can assure you.


CAB Example
Example from the OrSaveIt App
The technical reason behind the crash is that when you double tap or long click on a word in an EditText it launches a CAB for copy/paste. This closes the CAB that you have launched where the logic to manage the edit mode state and change the EditText back into TextView lives. And trying to edit something that no longer exists does not go down well.

It will give you a nice NullPointerException that looks something like this (Depending on the API level of the device the Editor.java line will change):


1:  E/AndroidRuntime(24518): java.lang.NullPointerException  
2:  E/AndroidRuntime(24518): at android.widget.Editor$SelectionActionModeCallback.onCreateActionMode(Editor.java:2757)  

There is a solution to this problem! Don't use a CAB. Instead take a leaf out of the Ice cream Sandwich contacts application and use the regular ActionBar but with a custom view acting as your up affordance.


ICS contacts app
ICS Contacts App

I know what you are all really after here is some sample code, so here it is:


Comments

Popular Posts