在片段android中查看重用

在片段android中查看重用,第1张

概述我试图在我的片段中保存我的视图状态,但我担心我会泄漏我的活动.这是我在做的事情: @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state){ if(mView != null){ View oldParent = mView.getParent(); 我试图在我的片段中保存我的视图状态,但我担心我会泄漏我的活动.这是我在做的事情:
@OverrIDepublic VIEw onCreateVIEw(LayoutInflater inflater,VIEwGroup container,Bundle state){   if(mVIEw != null){      VIEw oldParent = mVIEw.getParent();      if(oldParent != container){         ((VIEwGroup)oldParent).removeVIEw(mVIEw);      }      return mVIEw;   }   else{      mVIEw = inflater.inflate(R.ID.fragvIEw,null)      return mVIEw;   }}

我很担心,因为我知道所有的视图都保留在上下文中,如果从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.

使用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.

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

总结

以上是内存溢出为你收集整理的在片段android中查看重用全部内容,希望文章能够帮你解决在片段android中查看重用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1139154.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存