Snippets and onConfigurationChanged

I am trying to do something that I am doing with actions, but inside a fragment. I use actions:

First stop restarting activity when android:configChanges="keyboardHidden|orientation|screenSize" device is spinning android:configChanges="keyboardHidden|orientation|screenSize"

in my activity add:

 @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); setContentView(R.layout.main); } 

So, so that the activity does not restart, but main.xml is reloaded, in order to use the layout earth

Now I have an activity showing a viewpager that contains three fragments. Everything is working correctly. Rotation detection occurs in fragments

 public class FRG_map_web extends Fragment { @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); Log.i("myLogs", "Rotation"); } 

The problem is that the fragment does not use setContentView (R.layout.main); this is the code:

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.frg.myFragment, null); 

I tried using:

 LayoutInflater inflater = inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.frg.myFragment, null); ... LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.frg.myFragment, null); ... LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE ); view = inflater.inflate(R.layout.frg.myFragment, null); ... LayoutInflater li = LayoutInflater.from(context); 

and in different ways, but always without success. I canโ€™t swell right.

Can someone tell me how should I do this?

Thanks in advance for your help.

Hi

+6
android android-fragments layout-inflater
source share
4 answers

If I understand correctly, you do not want the fragment to be loaded at every turn. Instead, you want to transfer views and update them with existing information.

 public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // Get a layout inflater (inflater from getActivity() or getSupportActivity() works as well) LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View newView = inflater.inflate(R.layout.frg.myFragment, null); // This just inflates the view but doesn't add it to any thing. // You need to add it to the root view of the fragment ViewGroup rootView = (ViewGroup) getView(); // Remove all the existing views from the root view. // This is also a good place to recycle any resources you won't need anymore rootView.removeAllViews(); rootView.addView(newView); // Viola, you have the new view setup } 

According to the documentation ( getView () ) getView () returns the same view that you returned from your onCreateView (), but it is not. It actually returns the parent of the view you returned to onCreateView (), which is exactly what you need. getView () will return an instance of NoSaveStateFrameLayout, which is used specifically for Fragments as the root view.

Hope this helps.

+25
source share

Have you considered saving instances of a fragment? See Snippet # setRetainInstance .

Allow your activity to recreate (do not specify android: configChanges), but keep the fragment instances in a change of orientation. If all heavy lifting happens in Fragment # onCreate, this should work fine. onCreate () will not be called again since the fragment will not be re-created.

+14
source share

I could do this by re-linking the fragment inside onConfigurationChanged:

  @Override public void onConfigurationChanged(Configuration newConfig) { getActivity().detachFragment(this); super.onConfigurationChanged(newConfig); .... getActivity().attachFragment(this); } 

Remember that by separating and linking a fragment, you will only work with its presentation. But the state of the fragment is โ€œsavedโ€ in the fragment manager.

+2
source share

Perhaps you can try using

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.frg.myFragment, container, false); } 

. In any case, the fragment still needs to be destroyed and recreated, why not let Android process it automatically by restarting it? If any data is saved, you can save it to onSavedInstanceState() . Setting android:configChanges="keyboardHidden|orientation|screenSize" not recommended in Android.

0
source share

All Articles