<p>I'm writing a mobile phone game using j2me. In this game, I am using multiple Canvas objects.
For example, the game menu is a Canvas object, and the actual game is a Canvas object too.
I've noticed that, on some devices, when I switch from one Canvas to another, e.g from the main menu to the game, the screen momentarily "flickers". I'm using my own double buffered Canvas.</p>

<p>Is there anyway to avoid this?</p>

[{'answer_id': 66742, 'author': 'Vivek', 'author_id': 7418, 'author_profile': 'https://Stackoverflow.com/users/7418', 'pm_score': 0, 'selected': False, 'text': '<p>Do you use double buffering? If the device itself does not support double buffering you should define a off screen buffer (Image) and paint to it first and then paint the end result to the real screen. Do this for each of your canvases. Here is an example:</p>\n\n<pre><code>public class MyScreen extends Canvas {\n   private Image osb;\n   private Graphics osg;\n   //...\n\n   public MyScreen()\n   {\n         // if device is not double buffered\n         // use image as a offscreen buffer\n         if (!isDoubleBuffered())\n         {\n            osb = Image.createImage(screenWidth, screenHeight);\n            osg = osb.getGraphics();\n            osg.setFont(defaultFont);\n         }\n   }\n\n   protected void paint(Graphics graphics)\n   {\n      if (!isDoubleBuffered())\n      {\n         // do your painting on off screen buffer first\n         renderWorld(osg);\n\n         // once done paint it at image on the real screen\n         graphics.drawImage(osb, 0, 0, Tools.GRAPHICS_TOP_LEFT);\n      }\n      else\n      {\n         osg = graphics;\n         renderWorld(graphics);\n      }\n   }\n}\n</code></pre>\n'}, {'answer_id': 67622, 'author': 'Shane Breatnach', 'author_id': 10264, 'author_profile': 'https://Stackoverflow.com/users/10264', 'pm_score': 0, 'selected': False, 'text': "<p>A possible fix is by synchronising the switch using Display.callSerially(). The flicker is probably caused by the app attempting to draw to the screen while the switch of the Canvas is still ongoing. callSerially() is supposed to wait for the repaint to finish before attempting to call run() again.</p>\n\n<p>But all this is entirely dependent on the phone since many devices do not implement callSerially(), never mind follow the implementation listed in the official documentation. The only devices I've known to work correctly with callSerially() were Siemens phones.</p>\n\n<p>Another possible attempt would be to put a Thread.sleep() of something huge like 1000 ms, making sure that you've called your setCurrent() method beforehand. This way, the device might manage to make the change before the displayable attempts to draw.</p>\n\n<p>The most likely problem is that it is a device issue and the guaranteed fix to the flicker is simple - use one Canvas. Probably not what you wanted to hear though. :)</p>\n"}, {'answer_id': 68854, 'author': 'JaanusSiim', 'author_id': 706, 'author_profile': 'https://Stackoverflow.com/users/706', 'pm_score': 3, 'selected': False, 'text': '<p>I would say, that using multiple canvases is generally bad design. On some phones it will even crash. The best way would really be using one canvas with tracking state of the application. And then in paint method you would have</p>\n\n<pre><code>protected void paint(final Graphics g) {\n  if(menu) {\n    paintMenu(g);\n  } else if (game) {\n    paintGame(g);\n  }\n}\n</code></pre>\n\n<p>There are better ways to handle application state with screen objects, that would make the design cleaner, but I think you got the idea :)</p>\n\n<p>/JaanusSiim  </p>\n'}, {'answer_id': 86154, 'author': 'Honza', 'author_id': 8621, 'author_profile': 'https://Stackoverflow.com/users/8621', 'pm_score': 0, 'selected': False, 'text': '<p>It might be a good idea to use GameCanvas class if you are writing a game. It is much better for such purpose and when used properly it should solve your problem.</p>\n'}, {'answer_id': 318134, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>Hypothetically, using 1 canvas with a sate machine code for your application is a good idea. However the only device I have to test applications on (MOTO v3) crashes at resources loading time just because there's too much code/to be loaded in 1 GameCanvas ( haven't tried with Canvas ). It's as painful as it is real and atm I haven't found a solution to the problem. \nIf you're lucky to have a good number of devices to test on, it is worth having both approaches implemented and pretty much make versions of your game for each device.</p>\n"}]
['https://Stackoverflow.com/questions/66438', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/9771/']