Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6884085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:27:50+00:00 2026-05-27T05:27:50+00:00

Not sure this is an android issue, I am a c++ developer trying to

  • 0

Not sure this is an android issue, I am a c++ developer trying to write java 🙂

The problem in brief..
I have replaced a long and hard to maintain switch case with
a finite state machine and while the base code is the same I get
about 20% performance loss

Some more detailed explanation:
I’ve create a SurfaceView based andriod app based on the sample code from SDK
after extending my app I found myself maintaining long switch cases on my game loop

generally my app skeleton looks like this (with many cases)

class myView extends SurfaceView implements SurfaceHolder.Callback {
   public void update() {
      switch (_state) {
         case MENU: ... break;
         case LOAD: ... break;
         case RUN: ... break;
         ...
      }
   }

   public void draw(Canvas c) {
      switch (_state) {
         case MENU: ... break;
         case LOAD: ... break;
         case RUN: ... break;
         ...
      }
   }

   public boolean onTouchEvent(MotionEvent event) {
      switch (_state) {
         case MENU: ... break;
         case LOAD: ... break;
         case RUN: ... break;
         ...
      }
   }
}

So I decided to refactor my code and implemented a simple finite state machine (FSM)

public class StateMachine {

    public State state;
    private myView context;

    public StateMachine(myView view) {
        context = view;
    state = new StateMainMenu();
}

    interface State {
        public void update();
        public void draw(Canvas canvas);
        public boolean onTouchEvent(MotionEvent event);
    }

    class StateMainMenu implements State {

    public StateMainMenu() { ... }

    @Override
    public void update() { 
            ... 
            if (windOfChange) {
               state = new StateTheNextState();
            }
        }

        @Override
    public void draw(Canvas canvas) { ... }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
            synchronized (_thread.getSurfaceHolder()) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN: ... break;
                    ...
                }
                return true;
            }
        }
    }
}

and in myView is used it like this

class myView extends SurfaceView implements SurfaceHolder.Callback {

    private StateMachine stateMachine = new StateMachine(this);

    public void update() {
        stateMachine.state.update();
    }

    public void draw(Canvas c) {
        stateMachine.state.draw(c);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        synchronized (_thread.getSurfaceHolder()) {
            return stateMachine.state.onTouchEvent(event);
        }
    }
}

While, inside the states I do exactly what I did before
same amount of objects creations and the very same code
running my app I get about 20% less frame rates…

Any ideas why? or what am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:27:50+00:00Added an answer on May 27, 2026 at 5:27 am

    My advice would be to profile your app’s execution with Traceview to identify which part of it is acting as a bottleneck. Without that, it’s a bit like shooting in the dark.
    I would put my 2 cents on the synchronized blocks though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Not sure this is possible, but looking to write a script that would return
I'm sure this is a fairly trivial problem, but I'm not sure what to
I have a question this time around regarding the Android custom camera, NOT the
I have developed a compass based on this example: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/Compass.html Since my app only
I have a bit of an issue that I am not sure how to
I'm in not a java guy, so I'm not sure if this is just
Not sure this is a programming question, but we use LaTeX for all our
I'm not sure this is possible, but in ruby, you can dynamically call a
I'm not sure this is possible, but is there a syntax to be used
I'm still not sure this is the correct way to go about this, maybe

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.