Skip to main content

Command Palette

Search for a command to run...

An implementation of Finite State Machine in Android using Handler...

Updated
2 min read
S
In search of WhoAmI...

Note:_If you want to know about a full-fledged application about how a long running task in a background service has been broken into small states please have a look atthis article. _

As i was doing some research on different concepts in Android, i came out with this implementation of Finite State Machine using the Handler class... i would like to share it with you... the state diagram of the application will look as the following:

The main Activity class looks like the following:

Here's the formatted version of your Java code for better readability:

package com.android.training.statepattern;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class StatePatternActivity extends Activity implements View.OnClickListener {
    private Button startStatePattern;
    private MyHandler handler;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        startStatePattern = (Button) findViewById(R.id.button1);
        startStatePattern.setOnClickListener(this);
        handler = new MyHandler(this);
    }

    public void onClick(View v) {
        if (v.equals(startStatePattern)) {
            handler.sendEmptyMessage(1);
        }
    }
}

This formatting includes proper indentation and spacing to enhance readability and maintain standard coding practices.

And the other class is as follows;

package com.android.training.statepattern;

import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

public class MyHandler extends Handler {
    private StatePatternActivity MainActivity;

    MyHandler(StatePatternActivity a) {
        this.MainActivity = a;
    }

    // State transition logic is strongly coupled with the different states
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case 1:
                // Do the processing
                Toast.makeText(MainActivity.getApplicationContext(), "Inside State 1", Toast.LENGTH_SHORT).show();
                this.sendEmptyMessage(2);
                break;
            case 2:
                // Do the processing
                Toast.makeText(MainActivity.getApplicationContext(), "Inside State 2", Toast.LENGTH_SHORT).show();
                this.sendEmptyMessage(3);
                break;
            case 3:
                // Do the processing
                Toast.makeText(MainActivity.getApplicationContext(), "Inside State 3", Toast.LENGTH_SHORT).show();
                this.sendEmptyMessage(4);
                break;
            case 4:
                break;
            default:
                break;
        }
    }
}

Let me explain the code a bit... here the crux of the solution lies in the MyHandler class derived from Handler.. The main activity class HAS MyHandler..

We start the State machine by sending a message (integer 1) through the function SendEmptyMessage called on MyHandler object. As a result, the callback function called handleMessage of the Handler is called and we handle this state's functionality here... the next all starte transitions happen sequentially from this callback function...

Hope this throws some lights on a specific design concept in Android...

3 views

More from this blog

Som's Tech World...

149 posts

I am in search of WhoAmI. This is my own world of technologies and software. I believe in the following simple fact of life... To win is no more than this... To rise each time you fall...