是否在 attach() 方法之后调用 detach() 方法? [英] Does detach() method call after attach() method?

查看:42
本文介绍了是否在 attach() 方法之后调用 detach() 方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 UI 类

I have a simple UI class

public class HelloWorldUI extends UI {

@Override
protected void init(VaadinRequest request) {
    System.out.println("Initialized !");
    final VerticalLayout layout = new VerticalLayout();
    layout.addComponent(new Label("Hello World !"));
    setContent(layout);
}

@Override
public void detach() {
    System.out.println("Detach !");
    super.detach();
}

@Override
public void attach() {
    System.out.println("Attach !");
    super.attach();
}
}

第一次加载我的 UI 时,我在控制台看到输出为

When first time my UI was loaded , I see outputs at my console as

附上!
已初始化!

Attach !
Initialized !

没关系,这正是我所期望的.但是当我刷新浏览器时,我的控制台输出是

It is OK and this is what I expected. But when I refresh the browser , my console outputs were

附上!
已初始化!
分离!

Attach !
Initialized !
Detach !

太棒了!我认为 Detach ! 可能首先产生,因为(我认为)当浏览器刷新时,detach() 方法应该被调用并且 attach() , init() 应该遵循.但实际上 detach() 方法会在 attach() 方法之后调用.我的想法有什么问题?

Amazing ! I think Detach ! may be produce first because (as I think) when browser was refreshed , detach() method should be call and attach() , init() should be follow . But actually detach() method will call after attach() method. What's wrong my thinking ?

推荐答案

Browser Refresh = New UI Instance

当您刷新浏览器窗口或选项卡时,一个新的UI 实例已创建.所以你会看到一个 new UI 实例的附加消息.旧的 UI 实例将在稍后分离.

Browser Refresh = New UI Instance

When you refresh a browser window or tab, a new UI instance is created. So you see an attach message of a new UI instance. The old UI instance will be detached later.

这是 Vaadin 7 中的默认行为.您可以使用注释更改该行为.

This is default behavior in Vaadin 7. You may change that behavior with an annotation.

UI 添加 @PreserveOnRefresh 注释code> 改变了行为:刷新时不会创建新的 UI 实例.

Adding @PreserveOnRefresh annotation to the UI changes the behavior: No new UI instance won't be created on refresh.

引用此注释的文档:

标记用户刷新浏览器窗口时应保留的 UI.默认情况下,刷新时会创建一个新的 UI 实例,从而导致任何未在 URL 或 URI 片段中捕获的 UI 状态被丢弃.通过将此注释添加到 UI 类,框架将在检测到重新加载时重用当前的 UI 实例.

Marks a UI that should be retained when the user refreshed the browser window. By default, a new UI instance is created when refreshing, causing any UI state not captured in the URL or the URI fragment to get discarded. By adding this annotation to a UI class, the framework will instead reuse the current UI instance when a reload is detected.

这篇关于是否在 attach() 方法之后调用 detach() 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆