View reuse in android fragments

I am trying to save my View states in my fragment, but I am worried that I will skip my activity. That's what I'm doing:

@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; } } 

I am worried that I know that all views are held in context, and I do not know if this is an Activity context or an application context if it is swollen from a blower. Perhaps it would be a better idea to pragmatically create a view and set its attributes using getActivity (). GetApplication (), not use a blower. I would appreciate any feedback on this.

Thanks!

EDIT: A confirmed activity leak, although this code works fine, don't do this: * (

+8
android memory-leaks android-fragments android-support-library
source share
3 answers

I am trying to save my View states in my fragment, but I am worried that I will skip my activity.

Use onSaveInstanceState() (in the snippet) and onRetainConfigurationInstance() (in action) to save the "view states" when changing the configuration. I'm not quite sure what other "View states" you can keep in mind.

I am worried that I know that all views occupy a context, and I do not know if this is an Activity context or an application context if it is pumped from an inflatable device.

Since using Application to view inflation does not work well, you should be bloated by Activity . And, therefore, the submissions will contain a reference to the activity that pumped them.

+2
source share

@schwiz Γ© implemented something similar.

I am using setRetainInstance in a fragment. on the fragment layout I have a FrameLayout placeholder where I put my webView inside.

This webView is created programmatically on Fragment onCreate using the application context, and I am doing addView(webView) on the bloated layout in onCreateView() .

 webView = new WebView(getActivity().getApplicationContext()); 

And on onDestroyView, I just delete the webView from my framelayout bookmark file.

It works very well if you are not trying to play the video in full screen. This does not work because it expects an activity context

+1
source share

I am trying to save View states in my fragment

To save and save view state, you can simply use View.onSaveInstanceState () and View.onRestoreInstanceState (Parcelable state)

This will help you save and restore the state of the view due to activity or fragment. See this answer for more information on this ( how to prevent custom views from changing due to screen orientation changes )

0
source share

All Articles