在片段android中查看重用

我试图在我的片段中保存我的视图状态,但我担心我会泄漏我的活动.这是我在做的事情:
@Override
public 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性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...