5/23/2014

Statecharts and Android Development

Lately I am so involved in statecharts, which is a formalism to define the behavior of a system, and can be thought as the extended version of finite state machines. You can read more here.

Statecharts can be used in many areas which have a behavior. I will give an example how statecharts made my life easier and my code more readable in android development.

For example when I was developing for android, I always have to write some code to the function "onBackPressed", which is the function called when you press the back button on your screen. Depending on your variables and program, this function can get bigger and bigger and every time you have to keep all of your variables in your mind and check them in this function. Actually the same case is true for all other to-be-overriden activity functions and your button's click functions.

I will just show an example of how my onBackPressed function was before statecharts. The following image is just an excerpt.
The code was going more lines like this. In this code, I was checking a timer everytime the user pressed back and asking if he wants to quit or not.

Then with the statecharts, I could seperate the behavior from the actual UI management code. In statecharts, you fire some event and the statechart will behave according to the events you defined in it. And you don't have to worry about what other variables will be. You will just write you api for GUI manipulation for example and let the statechart define the behavior.

So I designed the following statechart for my code. I will show only a portion for the sake of place. I designed the statechart in scxml-gui application, which is a very nice little tool for statechart visualization.
As you see, there are some states and some events are changing the states. And maybe you also recognized the 'backPressed' event from some states to some others.

Now the following is the new onBackPressed function of my code.
Yes that's right. This is all the code we need in the function and in all other functions we added to the onclick attributes of each button. The function will just fire a 'backPressed' event and the statechart will take care of the event if it is applicable from the current state. So in the statechart above, if the statechart is in the 'help' state and user presses back button, the app will go to the 'drawSetupLayout' state.

This may sound hard at the beginning but when you get used to define your behavior with statecharts, I am sure you will want to use it more and more.

I have produced a sample android application (Traffic Lights Simulator) by using the java implementation of UML statecharts by klangfarbe. He has done a decent job by the way. You can try the application and see the source code from here.

Also the statechart I give in this post is belong to this Taboo game application. I didn't open-source the code for that one but I have only one activity, a GUI api and the statechart. Altogether very nice. Also in another post, I will show how to create a StatechartButton specifically for firing events in xml, instead of writing a regular function and firing the event in that function.

Thanks to Eugene (my advisor) and Conner (my old project and school mate) for introducing the statecharts to me and forcing me to use them :) If you have a question, just shoot a comment.

No comments:

Post a Comment