我有一个服务类,我将一个监听器附加到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);
}
}