Skip to main content

Form navigation question

6 replies [Last post]
Anonymous

Hello.
I am a new LWUIT user, so to start with, I would like to say a Big Thanks to the team that created it and the community. I don't have much experience with mobile development, and started only recently, with a n application for my own uses. The first approach was with pure MIDP UI classes, and sometimes the screen changing logic got really twisted. Now, with LWUIT, the code is much cleaner, and as I have some experience with Swing, my life is easier.

Suppose I have two forms: FormOne and FormTwo. FormOne has a button whose listener looks like this:

<br />
new SecondForm(FirstForm.this).show();<br />

so it creates a new instance of SecondForm, passes itself as the "previous form", and shows it.
No, second form has a back button, whose action is the following:
<br />
previousForm.show();<br />

The question is, whether this is the correct way to do form navigation? I have serious doubts about this. The first form shows the second, and the second form may show the first one as it has the reference to it, and then the first can create the new second form and show it, I can go back, and so on. The second form seems not to be ever disposed (I don't know what the Form.show() method does), so if I do the switching between forms long enough, I might get an OutOfMemoryError, right?
I can create a single instance of the second form, and reuse it in the first one, but I think that if I switch the forms long enough (probably much much longer), I could get a StackOverflowError (if it exists in JME), as the forms would be showing themselves and the calls to show() would be put on the stack.
For dialogs, I can call dispose() and it just disappears, how do you do this correctly for forms?
Please explain to me the flow of the navigation.

Second question is: what does the showBack() method do? How is it different? I don't fully understand what the docs say, and when I use it it seems not to be any different from show().

Thanks and regards,
szczyp

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
vprise
Offline
Joined: 2003-11-07
Points: 0

Hi,
generally in MIDP we recommend having a "state machine" and navigating between forms through a form id (a controller for potential destinations).

showBack works just like show() except the transition goes in the reverse direction, e.g. slide transition would move the other way around mostly useful to visualize "back" key functionality.

szczyp

Thank you for your answers.
Could you describe or provide a link what such a state machine would look like? Pseudocode or just snippets would be fine.
Do you mean that there would be a main object, the application, whose state would be of type Form, the various states would be my custom forms, whose next/previous methods would change between different forms? Basically the state pattern? Should I create all the forms up-front or what do you suggest?
Sorry for such maybe stupid questions, but I am very inexperienced when it comes to JME development, and simply don't know the dos and do-nots.

Regards,
szczyp

vprise
Offline
Joined: 2003-11-07
Points: 0

Generally have a "controller" like class that accepts generic requests such as "go to form X with state Y" and also keeps a log of previous requests (so you can go "back"). This might look like this:

navigateTo(int destination, Object state);

This controller will create the form (although it doesn't have to contain the logic for creation/population) and would return it. The main use of this class is as a single point of controlling the navigation within the application.

szczyp

Thank you for your answer.
I don't fully understand. So, when I'm in form one and want to go to form two, I use the controller. But, this still would show the next form on top of the current one, and it was possible to overflow the stack. I created a simple application that shows the first form, then shows the second, than shows the first and so on, and it actually does what I would have expected - a stack overflow (of course, it needs many many calls, but still). There should be some way to dispose/hide the current form, and only then show the next one, but I cannot find any hide() / dispose() method for the Form class. Or maybe the LWUIT Display object does this?
Please help.

sczyp

szczyp

Ok, I think understood something.
Am I correct in saying that once a form is shown, and then, let's say that as a result of a command, another form's show() is called, and no references exist to the current form, it is disposed, and can be garbage collected? When I call show and show and show again, the forms are NOT layered on top of each other, they are replaced, and can be GC-ed?
Regards,
szczyp

vprise
Offline
Joined: 2003-11-07
Points: 0

Yes.
The idea is to create a new form instance every time to conserve memory as much as possible.