我有一个 Service 类,我在其中附加了一个侦听器MobileApplication.viewProperty
来跟踪以前的/当前的View
。当我切换回另一个时,View
我想检查 this 的名称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);
}
}