Fragment backstack bug when replacing fragments in a transaction?
Here's the scenario that causes problems:
This exception is thrown by Fragment A, because I'm saving some values of Views (EditTexts eg) in its onSaveInstanceState() method. It seems, that on the second rotation, the system doesn't instantiate these Views, so when I'm asking their values, I get a NullPointer exception. Can this be avoided somehow? Or is using .replace operations in a fragment transcation saved onto the backstack unadvised?
I've had this but can't quite recollect the specifics of what I did to fix but generally speaking (and apologies for the brain dump) I do the following:
Call setRetainInstance(true)
in onCreate
to avoid fragment recreation
Save the state of edit controls etc. in onSaveInstanceState
to be used if activity is killed and you get restored with a non-null bundle (you shouldn't get a non-null bundle on an orientation change with 1.)
Maintain edit control values in member variables (as the fragment is not going to be recreated) ensuring they get updated in an onDestroyView
from the edit controls and then use them to restore the edit control values in onCreateView
Have a flag which I set to true in onCreateView
and false in onDestroyView
and ensure I don't touch UI controls when the view is not around.
BTW Using replace
while adding the transaction to the back stack is perfectly OK.
Hope there's something in there that helps. Peter.
链接地址: http://www.djcxy.com/p/10062.html上一篇: 为什么这个模板代码被允许违反C ++的私有访问说明符?
下一篇: 在交易中替换碎片时的碎片堆栈错误?