如果更改太快,则在方向更改 FC 的应用程序时保存字符串
我在保存方向更改时遇到问题。我尝试使用 onSaveInstanceState()/onRestoreInstanceState()
和 onRetainNonConfigurationInstance()/getLastNonConfigurationInstance()
但没有成功。
我有:
@Override
public String onRetainNonConfigurationInstance(){
final String savedType = currentType;
return savedType;
}
并且在 onCreate()
中我有:
currentType = (String) getLastNonConfigurationInstance();
这对我来说还不起作用,并且在方向更改后 currentType
始终为 null
。有什么建议吗?
修订
所以这就是我目前所拥有的,但它仍然不起作用:
@Override
protected void onSaveInstanceState(Bundle outState){
outState.putString("savedType", currentType);
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState){
currentType = savedInstanceState.getString("savedType");
super.onRestoreInstanceState(savedInstanceState);
}
我尝试将 super
方法调用放在我能想到的每个组合中的每个方法的开头和结尾,但仍然得到一个再次创建视图时,currentType
出现 NullPointerException
。
另外,
这是在对话框中发生的,我不确定这是否与之有关,但认为这可能是有用的信息。
部分解决方案/部分新问题
所以我的工作有点像我想要的。方法中隐藏了一条语句,该语句在生命周期结束时将 currentType
设置为 null
,此时不再需要该变量。现在已经不成问题了。屏幕将成功改变方向,但如果在两者之间连续改变得太快,则会发生FC。这就是我用来让它在这种状态下工作的方法:
类字段:
private String currentType;
在onCreate
结束时我有:
currentType = (String) getLastNonConfigurationInstance();
然后:
@Override
public Object onRetainNonConfigurationInstance() {
final String s = currentType;
return s;
}
如果方向不改变然后很快又变回来,它就可以正常工作。我想解决这个问题,因为百货公司和零售店正在使用/将使用这个,我可以看到人们掉落它,这将导致 FC(将它掉落在我的沙发上进行测试)。我只存储一个字符串,因此我存储的内容不会占用太多内存。针对这种新情况有什么建议吗?
I'm having trouble saving a string on orientation changed. I've tried using onSaveInstanceState()/onRestoreInstanceState()
and onRetainNonConfigurationInstance()/getLastNonConfigurationInstance()
with no luck.
I have:
@Override
public String onRetainNonConfigurationInstance(){
final String savedType = currentType;
return savedType;
}
and in onCreate()
I have:
currentType = (String) getLastNonConfigurationInstance();
This hasn't worked for me yet and currentType
is always null
after an orientation change. Any suggestions?
Revision
So this is what I currently have and it's still not working:
@Override
protected void onSaveInstanceState(Bundle outState){
outState.putString("savedType", currentType);
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState){
currentType = savedInstanceState.getString("savedType");
super.onRestoreInstanceState(savedInstanceState);
}
I've tried putting the super
method calls at the beginning and end of each method in every combination I could think of but am still getting a NullPointerException
for currentType
when the view is created again.
Also
This is happening in a Dialog, I'm not sure if that has anything to do with it but thought that might be a useful bit of info.
Partial Solution/Partial New Question
So I got this working somewhat like I wanted. There was a statement buried in a method that set currentType
to null
towards the end of the life cycle when the variable wouldn't be needed again. That's now out of the way. The screen will successfully change orientations but if it's changed between the two successively too quickly it will FC. This is what I used to get it working in this state:
Class Field:
private String currentType;
At the end of onCreate
I have:
currentType = (String) getLastNonConfigurationInstance();
And then:
@Override
public Object onRetainNonConfigurationInstance() {
final String s = currentType;
return s;
}
It works fine if the orientation doesn't change and then change back again quickly. I'd like to fix this though because department and retail stores are using/will use this and I can see people dropping it, which will cause an FC (dropped it on my couch to test). I'm only storing one string so what I'm storing doesn't take up much memory. Any suggestions for this new situation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过将其放入捆绑包中?
Have you tried putting it in a Bundle?
所以我让这个工作有点像我想要的。方法中隐藏了一条语句,该语句在生命周期结束时将 currentType 设置为 null,此时不再需要该变量。现在已经不成问题了。屏幕将成功改变方向,但如果在两者之间连续改变得太快,则会发生FC。这就是我用来让它在这种状态下工作的方法:
类字段:
在 onCreate 结束时我有:
然后:
如果方向没有改变,然后很快又变回来,它就可以正常工作。提出关于这个新错误的新问题,
So I got this working somewhat like I wanted. There was a statement buried in a method that set currentType to null towards the end of the life cycle when the variable wouldn't be needed again. That's now out of the way. The screen will successfully change orientations but if it's changed between the two successively too quickly it will FC. This is what I used to get it working in this state:
Class Field:
At the end of onCreate I have:
And then:
It works fine if the orientation doesn't change and then change back again quickly. Asking new question about this new bug,