Skip to main content

Display - Best practice

2 replies [Last post]
peter_budo
Offline
Joined: 2006-10-18
Points: 0

What is best practice in handling movement from screen to screen in midlet?
My teacher at university wasn't able to explain this to me, could be due to that he chuck whole 300+ lines in one file.
Secondly none of the books or tutorials that I read so far went on discussing this.
So here is how I do it and I would like to know if my approach is correct and if not I would like to know how to handle this correctly.

ImageHttp.java

 package imageHttp;</p>
<p>import javax.microedition.lcdui.Display;<br />
import javax.microedition.midlet.MIDlet;</p>
<p>import imageHttp.gui.MainMenu;</p>
<p>public class ImageHttp extends MIDlet{</p>
<p>    public static ImageHttp instance;<br />
    private MainMenu mMenu;</p>
<p>    public ImageHttp(){<br />
        instance = this;<br />
    }</p>
<p>    public void startApp(){<br />
        if(mMenu == null){<br />
            mMenu = new MainMenu();<br />
        }<br />
        Display.getDisplay(this).setCurrent(mMenu);<br />
    }</p>
<p>    public void pauseApp(){}</p>
<p>    public void destroyApp(boolean unconditional){}</p>
<p>    public static void quitApp(){<br />
        instance.destroyApp(true);<br />
        instance.notifyDestroyed();<br />
        instance = null;<br />
    }<br />
}

MainMenu.java

package imageHttp.gui;</p>
<p>import javax.microedition.lcdui.Command;<br />
import javax.microedition.lcdui.CommandListener;<br />
import javax.microedition.lcdui.Display;<br />
import javax.microedition.lcdui.Displayable;<br />
import javax.microedition.lcdui.List;</p>
<p>public class MainMenu extends List implements CommandListener{<br />
    private final Command exitCom;</p>
<p>    public MainMenu(){<br />
        super("Image Up/Down-load", List.IMPLICIT);</p>
<p>        exitCom = new Command("Exit",Command.EXIT,0);<br />
        addCommand(exitCom);<br />
        setCommandListener(this);<br />
    }</p>
<p>    public void commandAction(Command c, Displayable d){<br />
        if(c == exitCom){<br />
            imageHttp.ImageHttp.quitApp();<br />
        }<br />
        else{<br />
            int index = getSelectedIndex();<br />
            Displayable dis =  null;<br />
            switch(index){<br />
                case 0:<br />
                    dis = new ImageUrlRequest();<br />
                    break;<br />
                case 1:<br />
                    dis = new ImageServletRequest();<br />
                    break;<br />
                case 2:<br />
                    dis = new ImageUpload();<br />
                    break;<br />
                default:<br />
                    break;<br />
            }<br />
            Display.getDisplay(imageHttp.ImageHttp.instance).setCurrent(dis);<br />
        }<br />
    }<br />
}

In my opinion this better solution then sending instance of Displayable argument to next class.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
peter_budo
Offline
Joined: 2006-10-18
Points: 0

sfitzjava thank you for explanation, it was very helpful

sfitzjava
Offline
Joined: 2003-06-15
Points: 0

doing all of these static calls, and memory segment changes to address remotely located variables is not only a lot of typing but also can cause performance issues on small devices.

There are dozens of possible ways to address this depending on your desires. I use my project MicroBus.dev.java.net to handle this decoupling of classes by use of messaging. On one side I have an event, which fires a message, the other an object that registers for listening to for given messages. That would be major overkill for your needs :)

To help with performance/addressing issues you can add an instance variable of "private Display disp = Display.getDisplay(this);" to the ImageHttp class. Then by adding a method to change the current screen like:
public void changeScreen(Displayable _ui) { disp.setCurrent(_ui); }

Then your screen change logic goes from:
Display.getDisplay(imageHttp.ImageHttp.instance).setCurrent(disp);
to:
ImageHttp.instance.changeScreen(disp);

It's a little shorter code, but it removes the need to constantly call into the Display object, while changing the addressing to get the ImageHttp instance and then switch back to the Display Object to set the current UI.

However you will find that each screen may need to have its own commandListener which will make your app get really messy real quick. Making each screen then it's own CommandListener is going to be a better organization of logic, and here is a way that can help automate some construction logic by passing the class name and then you could have your ImageHttp pass an instance of itself to the class and not have to make a static variable of itself. This would require all screens you build have an interface, I'll call it CoreUI, and have one method: "void setUIManager(ImageHttp _mgr) ;"
Each Screen class would need to set itself as the commandListener and then implement CoreUI with an instance variable of type ImageHttp that I'll call mgr and the setUIManager implementation:
public void setUIManager(ImageHttp _mgr) { mgr =_mgr;}

Then to change the view all they need to do is call:
mgr.changeScreen("imageHttp.gui.ImageUrlRequest");
or if you had your screen classes defined in the CoreUI as constants then:
mgr.changeScreen(CoreUI.URL_REQUEST);

Then your ImageHttp class would have this changeScreen() method:

public void changeScreen(String _uiClass)
{
Displayable display = null;
try
{ display = (DIsplayable)Class.forName(_uiClass).newInstance();
disp.setCurrent(display);
((CoreUI)display).setUIManager(this);
}catch(Exception e){}
}

Then in ImageHttp's startApp() method instead of building the MainMenu class you use:
changeScreen("imageHttp.gui.MainMenu");
or if you setup the constant in CoreUI:
changeScreen(CoreUI.MAIN_MENU);

Following this style will also allow you to not have the static "instance" variable, and the quitApp() method can go from a class method to an instance method. (no more statics in your app)