Android 在配置更改后保留回调状态

发布于 2024-11-07 21:05:22 字数 529 浏览 4 评论 0原文

我非常了解 Android 生命周期。我在这里发帖是因为我观察到一种奇怪的行为,无论如何这是我自己的想法。

我的情况是这样的:一个活动将使用只有一个 EditText 的简单布局。在活动 onCreate 方法中,我为 EditText 设置了一些默认文本,并在该方法的后面部分,为 TextWatcher 分配了一个 TextWatcher 。 >EditText 因此,每当用户输入任何内容时,我都可以以自己的方式进行响应。

一切都很好,直到我旋转屏幕。 TextWatcher 回调开始对初始化 EditText 的代码做出反应。

按照正常的代码流程,TextWatcher是在初始化EditText的文本值之后分配的。因此,由于 onCreate 方法中的文本分配,它不应该被触发。

这里有人能解释一下吗?

I understand pretty well about Android lifecycle. I post here because I've observed one weird behavior, anyway this is my own thought.

My case is like this: One activity will use a simple layout with just a single EditText. In the activity onCreate method, i set some default text to the EditText and in later part of the method, assign a TextWatcher to the EditText so whenever user types in anything, i can response in my own way.

Everything is alright until I rotate the screen. The TextWatcher callback starts to react against the code that initialize the EditText.

According to the normal code flow, the TextWatcher is assigned later after initializing text value of the EditText. so it's not supposed to fire because of the text assignment in the onCreate method.

Could anyone here explain this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽你眉间 2024-11-14 21:05:22

这就是您看到此行为的原因:

  1. 首次调用“onCreate”方法时,它没有保存状态。这意味着 Bundle savedInstanceState 参数为 null。
  2. 当配置更改时,Android 在 savedInstanceState 参数中路径非空值。
  3. 配置更改后,onCreate 返回,Android 调用 onRestoreInstateState
  4. 默认情况下,所有具有 id 的视图都会尝试恢复其状态,EditText 也会恢复其状态(实际上,恢复大部分内容的 TextView它)。
  5. 在状态恢复期间的某个地方(但在 onCreate 方法完成之后),您的 EditText 控件对自身调用 setText 以恢复它所具有的文本就在配置更改之前。
  6. 您在 onCreate 方法中添加的 TextWatcher 会收到有关此更改的通知。

只是为了澄清这一点,您在第一次调用 onCreate 时添加的 TextWatcher 不会被保留!它的 TextWatcher 是在上次调用onCreate 时添加的,它接收文本更改通知。

您可以检查 TextView.onRestoreInstanceState 自己。

Here is why you see this behavior:

  1. When 'onCreate' method is called first time, it has no saved state. Which means Bundle savedInstanceState parameter is null.
  2. When configuration changed, Android paths non-null value in savedInstanceState parameter.
  3. After configuration is changed, and onCreate returns, Android calls onRestoreInstateState.
  4. By default, all views that have id are trying to restore their state, EditText restores its state too (actually, that TextView who restores most of it).
  5. At some place during state restoration (but after onCreate method is completed) your EditText control calls setText on himself in order to restore text that it had just before configuration changed.
  6. Your new TextWatcher that you added in onCreate method is notified about this change.

Just to make this clear, your old TextWatcher, that you added on first call to onCreate, is not preserved! Its new TextWatcher, that was added on last call to onCreate, which receives the text change notification.

You can examine TextView.onRestoreInstanceState yourself.

以往的大感动 2024-11-14 21:05:22

当您旋转设备时,活动的 onCreate 方法会再次调用。如果您想防止在旋转设备时调用 onCreate,请在清单

android:configChanges="orientation|keyboardHidden"

和活动中执行以下代码

public void onConfigurationChanged(Configuration newConfig)
{       
    super.onConfigurationChanged(newConfig);    

    if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE)
    {

    }      
    else if(newConfig.orientation==Configuration.ORIENTATION_PORTRAIT)
    {

    }  
}

When ever you rotate the device the onCreate method of activity called again.if you want to prevent to call onCreate when rotating the device do the following code in the Manifest

android:configChanges="orientation|keyboardHidden"

and in your activity

public void onConfigurationChanged(Configuration newConfig)
{       
    super.onConfigurationChanged(newConfig);    

    if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE)
    {

    }      
    else if(newConfig.orientation==Configuration.ORIENTATION_PORTRAIT)
    {

    }  
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文