codenameone / CodenameOne

Cross-platform framework for building truly native mobile apps with Java or Kotlin. Write Once Run Anywhere support for iOS, Android, Desktop & Web.
https://www.codenameone.com/
Other
1.7k stars 406 forks source link

Form shows title of previous Form #1771

Open SwingGuy1024 opened 8 years ago

SwingGuy1024 commented 8 years ago

Here's a skeleton of an application with a main window, a two-page wizard, and a settings page. It runs properly in the simulator, but bugs show up when I run it on my Android phone, As I proceed through the two pages of the wizard, the titlebar shows the name of the Form page. But when I return to the main page, either by completing the wizard or hitting the back button, the titlebar doesn't show the title of the main Form, it shows the title of the last page visited. This also happens if I go to the Settings page and back again.

Also, I get both a title bar and a tool bar, which I don't see when I run it in the simulator.

import java.io.IOException;
import com.codename1.io.FileSystemStorage;
import com.codename1.ui.Button;
import com.codename1.ui.Command;
import com.codename1.ui.FontImage;
import com.codename1.ui.Form;
import com.codename1.ui.Image;
import com.codename1.ui.animations.Transition;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.ui.plaf.Style;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.ui.Toolbar;

@SuppressWarnings("HardCodedStringLiteral")
public class Playground {

  private Form current;
  private MainForm mainForm;
  public void init(Object context) {
    UIManager.initFirstTheme("/theme");

    // Disable Toolbar on all Forms by default
    Toolbar.setGlobalToolbar(false);  // Change to true to restore the global Toolbar.

    mainForm = new MainForm();
  }

  public void start() {
    if (current != null) {
      current.show();
      return;
    }

    Form hi = mainForm;
    hi.show();
    current = hi;
  }

  public void stop() {
  }

  public void destroy() {
  }

  private class MainForm extends Form {
    MainForm() {
      super("Playground");
      setLayout(new BoxLayout(BoxLayout.Y_AXIS));
      Button startWizard = new Button("Start Wizard");
      startWizard.addActionListener(this::gotoToPageOne);
      add(startWizard);

      setToolbar(new Toolbar());                                  // comment me out to go to a global toolbar
      Command settingsCommand = new Command("Settings") {
        @Override
        public void actionPerformed(final ActionEvent evt) {
          navigateTo(mainForm, new SettingsForm(), null);
        }
      };
      getToolbar().addCommandToOverflowMenu(settingsCommand);

    }

    private void gotoToPageOne(final ActionEvent actionEvent) {
      Form pageOne = makePageOne();
      navigateTo(mainForm, pageOne, null);
    }
  }

  private Form makePageOne() {
    Form pageOne = new Form("Page One of Wizard");
    pageOne.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
    Button next = new Button("next");
    next.addActionListener((evt) -> navigateToPageTwo(pageOne));
    pageOne.add(next);
    return pageOne;
  }

  private void navigateToPageTwo(Form pageOne) {
    Form pageTwo = new Form("Page Two of Wizard");
    pageTwo.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
    Button done = new Button("Done");
    done.addActionListener((evt) -> {
      navigateTo(mainForm, mainForm, null);
    });
    pageTwo.add(done);
    navigateTo(pageOne, pageTwo, null);
  }

  private class SettingsForm extends Form {

    public SettingsForm() {
      super("Settings");
      setLayout(new BoxLayout(BoxLayout.Y_AXIS));

      Button colorButton = new Button("Background Color", createClearMaterialIcon(FontImage.MATERIAL_VISIBILITY));
      colorButton.getAllStyles().setAlignment(LEFT);
      colorButton.addActionListener((evt) -> selectColor());
      add(colorButton);
    }

    Image createClearMaterialIcon(char icon) {
      Style materialIconStyle = new Style();
      materialIconStyle.setBgTransparency(0, true);
      return FontImage.createMaterial(icon, materialIconStyle);
    }

    private void selectColor() {
    }
  }

  /**
   * This may also be used in place of Form.show(). This sets a backCommand into the specified destination Form
   * that will return the user to specified starting Form. Then it shows the destination form.
   *
   * @param startingForm The Form to return to when the user presses the back button
   * @param destination  The destination Form to show
   * @param transition   The transition to use, or null to use the existing transitions in the form.
   */
  static void navigateTo(final Form startingForm, final Form destination, Transition transition) {
    assert startingForm != null;
    assert destination != null;
    Command backCommand = createBackCommand(startingForm);
    if (transition != null) {
      startingForm.setTransitionOutAnimator(transition);
    }
    destination.setBackCommand(backCommand);
    destination.show();
  }

  static Command createBackCommand(final Form startingForm) {
    return new Command("Back") {
      @Override
      public void actionPerformed(final ActionEvent evt) {
        startingForm.showBack();
      }
    };
  }
}
chen-fishbein commented 8 years ago

The native theme does not play well with the Toolbar, I would suggest not mixing the two