更改设备方向时,在onCreate()中更改EditText的值无效 [英] Changing the value of an EditText inside onCreate() has no effect when changing device orientation
问题描述
我的理解是,当您更改设备方向时,您的应用会被破坏并重新创建.这意味着onCreate()应该在每次旋转设备时运行.
My understanding is that when you change the device orientation, your app gets destroyed and created again. This means that onCreate() should run every time I rotate the device.
我试图在每次创建应用程序时(包括在屏幕旋转时)设置一个EditText.因此,我启动了该应用程序,并且文本字段显示"Bap".然后,我将该字段的内容更改为"Boop",然后旋转屏幕.我希望该字段现在说"Bap",但事实并非如此,它仍然保持我更改后,方向更改之前的状态.为什么会这样?
I'm trying to set an EditText every time the app is created, including whenever the screen is rotated. So, I start up the app, and the text field says "Bap". I then change the contents of the field to "Boop", then rotate the screen. I would expect the field to now say "Bap", but it doesn't, it remains whatever it was after I changed it, before the orientation change. Why is this?
我的onCreate()看起来像这样:
My onCreate() looks like this:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText editText = (EditText)findViewById(R.id.edit_message);
editText.setText("Bap", TextView.BufferType.EDITABLE);
}
值得注意的是,如果将setText()调用移到onResume()上,一切都会按我期望的那样工作,并且每次旋转设备时都会重置文本字段.
It's worth noting that if I move the setText() call into onResume(), everything works as I would expect it to, with the text field resetting every time I rotate the device.
注意:我意识到提出了类似的问题,但给出的答案并不能真正解释为什么会发生这种行为.
NOTE: I realize a similar question has been asked, but the answer given doesn't really explain why this behaviour is occuring.
推荐答案
之所以发生这种情况,是因为Android框架正在执行应有的功能.它将保存一个捆绑包(SavedInstanceState),其中包含有关应用程序当前状态的所有信息,以便在方向更改后重新创建视图时使用.
It is happening because the Android framework is doing what it is supposed to. It is saving a bundle (SavedInstanceState) with all the information on the current state of your app, to be used when it re-creates your view after the orientation change.
您是否在发布的onCreate中遗漏了一些代码?也许像这样:
Did you leave some code out of the onCreate you posted? Maybe something like:
if (savedInstanceState == null)
如果将onCreate代码包裹在一个if中,这就是为什么看不到预期结果的原因.
If your onCreate code is wrapped in an if like that, that is why you are not seeing the results you expect.
具有如下所示的onCreate是标准做法:
It is standard practice to have an onCreate that looks like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (savedInstanceState == null) {
// Your first time start-up code here, will not run on orientation change
}
// Any code you want to run EVERY time onCreate runs goes here
}
这样做是为了避免不必要地在方向改变时重新创建所有内容.
This is done so you don't needlessly re-create everything on an orientation change.
希望这会有所帮助!
编辑
进一步研究,似乎正在发生什么(摘录自这里):
Looking into it further, it looks like what is happening is this (gleaned from here):
设备方向更改
onSaveInstanceState(Bundle outState)
onPause()
onStop()
onDestroy()
onCreate()
onStart()
onRestoreInstanceState(Bundle inState)
onResume()
基本上是说,捆绑软件将传递给onRestoreInstanceState
,该文件发生在onCreate
之后,这将说明您的情况.
It's basically saying that the bundle is getting passed to onRestoreInstanceState
, which occurs after onCreate
, which would explain what is happening in your case.
因此,解决方案是将EditText值的设置移动到您的onResume
方法(或覆盖onRestoreInstanceState
).
So, the solution would be to move the setting of the EditText value to your onResume
method (or overriding onRestoreInstanceState
).
这篇关于更改设备方向时,在onCreate()中更改EditText的值无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!