Gluon View生命周期

时间:2016-11-19 17:23:34

标签: gluon gluon-mobile

我有一个服务类,我将一个监听器附加到MobileApplication.viewProperty以跟踪前一个/当前View。 当我切换回另一个View时,我想检查此View的名称,以便根据它执行某些任务。 在视图onHidden处理程序中viewProperty已使用新视图值更新时,我的Service类中的侦听器将仅在onHidden之后调用。这可能是什么原因?

public class GluonApplication extends MobileApplication {

private static final String SECONDARY_VIEW = "secondary";

private String              previousView;
private String              currentView;

@Override
public void init() {

    addViewFactory(HOME_VIEW, () ->
    {
        Button btn = new Button("next view");
        btn.setOnAction(e -> switchView(SECONDARY_VIEW));

        View view = new View(new StackPane(btn)) {
            @Override
            protected void updateAppBar(AppBar appBar) {
                appBar.setTitleText("Home");
            }
        };

        return view;
    });

    addViewFactory(SECONDARY_VIEW, () ->
    {
        Button btnBack = new Button("go back");
        btnBack.setOnAction(e -> switchView("home"));

        View view = new View(new StackPane(btnBack)) {
            {
                setOnHidden(e ->
                {
                    System.out.println("viewProperty: " + viewProperty().get().getName() + " viewListener:" + currentView);

                    if (currentView.equals("myView")) {
                        // action
                    }
                });
            }

            @Override
            protected void updateAppBar(AppBar appBar) {
                appBar.setTitleText("Secondary");
            }
        };

        return view;
    });

    viewProperty().addListener((ov, v, v1) ->
    {
        if (v != null) {
            previousView = v.getName();
        }
        currentView = v1.getName();
        System.out.println("listener previousView: " + previousView + " view: " + currentView);
    });

}

@Override
public void postInit(Scene scene) {
    ((Stage) scene.getWindow()).setAlwaysOnTop(true);
}

}

0 个答案:

没有答案