在片段android中查看重用

前端之家收集整理的这篇文章主要介绍了在片段android中查看重用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在我的片段中保存我的视图状态,但我担心我会泄漏我的活动.这是我在做的事情:
  1. @Override
  2. public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle state){
  3. if(mView != null){
  4. View oldParent = mView.getParent();
  5. if(oldParent != container){
  6. ((ViewGroup)oldParent).removeView(mView);
  7. }
  8. return mView;
  9. }
  10. else{
  11. mView = inflater.inflate(R.id.fragview,null)
  12. return mView;
  13. }
  14. }

我很担心,因为我知道所有的视图都保留在上下文中,如果从inflater中膨胀,我不知道它是否是Activity上下文或Application上下文.也许更好的做法是使用getActivity().getApplication()而不是使用inflater来实际创建视图并设置其属性.我将不胜感激任何反馈.

谢谢!

编辑:确认活动泄漏,虽然这段代码工作得很好不要这样做:*(

解决方法

I am trying to save my View states in my fragment but I am concerned I make be leaking my Activity.

@H_502_17@

使用onSaveInstanceState()(在片段中)和onRetainConfigurationInstance()(在活动中)来维护配置更改中的“查看状态”.我不太确定你可能指的是其他“查看状态”.

I am concerned because I know all Views hold onto a context and I don’t know if it is the Activity context or Application context if inflated from the inflater.

@H_502_17@

由于使用应用程序进行视图通胀似乎不能正常工作,因此您应该从“活动”中进行充气.因此,这些观点将引用夸大它们的活动.

猜你在找的Android相关文章