title: How to go back to previous page with back button and different controllers? JAVAFX, JAVA
body: <p>I am fairly new to JAVAFX so please bear with me. Any help is greatly appreciated. Firstly, i have a Main app that loads addItems.fxml and addItemsController.</p>
  
  <pre><code>public class UncookedApp extends Application {
  
  private Stage stage;
  @Override
  public void start(Stage primaryStage) {
      stage = primaryStage;
  
      try {
          FXMLLoader loader=new FXMLLoader();
          loader.setLocation(getClass().getResource("/uncooked/view/addItems.fxml"));
          AnchorPane root=loader.load();
          addItemsController itemsCtrl=loader.getController();
          itemsCtrl.setMainApp(this);
          Scene scene=new Scene(root,1024,768);
          primaryStage.setScene(scene);
          primaryStage.show();
      } catch(Exception e){
          e.printStackTrace();
      }
  }
  
  public Stage getStage() {
      return stage;
  }
  
  public static void main(String[] args) {
      launch(args);
  }
  }
  </code></pre>
  
  <p>This is part of the addItemsController:</p>
  
  <pre><code>private UncookedApp mainApp;
  
  public UncookedApp getMainApp() {
      return mainApp;
  }
  public void setMainApp(UncookedApp mainApp) {
      this.mainApp = mainApp;
  }
  public void toMeat(ActionEvent event) {
      Stage stage1=mainApp.getStage();
      try {
          FXMLLoader loader=new FXMLLoader();
            loader.setLocation(getClass().getResource("/uncooked/view/addMeat.fxml"));
          Parent root=loader.load();
          addMeatCtrl itemsCtrl=loader.getController();
      //  itemsCtrl.setMainApp(this);
          Scene scene=new Scene(root,1024,768);
          stage1.setScene(scene);
          stage1.show();
      } catch(Exception e){
          e.printStackTrace();
      }
  
  }
  </code></pre>
  
  <p>The addItemsController will load a different page, addMeat. I got that to work but when I try to go back from addMeat to addItems with a button handle, it doesn't work. Is it something to do with retrieving the mainApp's stage? How can I solve this?</p>
  
  <p>This is what I've tried.</p>
  
  <pre><code>public void handleBackFromMeat(ActionEvent event) {
  try{
      Stage stage=mainApp.getStage();
      FXMLLoader loader=new FXMLLoader();
      loader.setLocation(getClass().getResource("/uncooked/view/addItems.fxml"));
      Parent root=loader.load();
      addItemsController itemsCtrl=loader.getController();
      Scene scene=new Scene(root,1024,768);
      stage.setScene(scene);
      stage.show();
  }catch (Exception e){
      e.printStackTrace();
  }
  
  
  }
  </code></pre>