Titanium mobile allows you to develop without worrying about native code, but that doesn’t mean that you should ignore native user expectations! The Android back button is a core piece of native android look and feel, and while users probably won’t notice when back behavior is done well, they’ll definitely notice when it’s done poorly. In this article, we’ll go through some tips and tricks that the Shockoe team and I have found useful for providing quick, intuitive, and maintainable android back behavior, and construct a simple application implementing these behaviors.
Treat your views like a stack
Or rather, maintain a stack of back behaviors that manage your view hierarchy. Users expect for the android back button to ‘undo’ their last major change in the app’s state, and using a callback stack is a very easy way to keep track of all of your user’s actions, in order. With this in mind, we can set up our app to handle back button presses gracefully from the first window.
This sets up your app to maintain a list of back behaviors, and once they have been exhausted close the app. Now let’s try adding another view.
And that’s it! Our simple little demo app is now set up to gracefully and consistently handle android back behaviors for a simple view hierarchy. Handling more complex view hierarchies will obviously require more complex android back handlers, we’ll go over handling the android back button in more complex apps in the next entry in this series.