Using onConfigurationChanged in a fragment
Asked Answered
L

1

18

I have this code in a fragment

public class TestOne extends Fragment {

    View view = null;

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);

      LayoutInflater inflater2 = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater2.inflate(R.layout.testone, null); 

        Toast.makeText(getActivity(), "Rotate fragment", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Toast.makeText(getActivity(), "onCreate Fragment", Toast.LENGTH_SHORT).show();

    }

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

        Toast.makeText(getActivity(), "onCreateView fragment", Toast.LENGTH_SHORT).show();

        return view; 
    }

}

What I'm trying to do is that, when I rotate the phone, I don't want the methods to be executed again. But I want to call again the xml layout, to load the layout-land folder's xml.

This code does not give any error, just does not work and do not understand the reason ..

I'm really interested in doing it using onConfiguratonChanged

I appreciate any help.

Thanks and regards

Lour answered 14/6, 2013 at 20:16 Comment(6)
the methods in your FragmentActivityElectroacoustics
there are methods that should be in the fragmentLour
And why should it work? In the onConfigurationChanged you simply inflate a layout and assign it to one of the fields of the Activity. I would advise you to use setRetainInstance(true) instead of the onConfigurationChanged way. As a manual solution I guess you could always remove all views from getView() and reattach the newly inflated layout.Mckenziemckeon
I've searched but can not find any example showing how to use setRetainInstance (true). It is advisable to use it?Lour
Please specifically describe how it fails. "just does not work" is not very clear.Melmon
Instead of using onConfigurationChanged you would set setRetainInstance() in the onCreate() method of the Fragment. This way the fragment instance will be kept across a configuration change, but the lifecycle methods will still be called in a slightly different order (so you would need to make some changes to them). If you just want to keep some data from being recreated then use a non UI fragment with setRetainInstance() to keep the data and let the fragment above be recreated(getting the data from that non UI fragment).Mckenziemckeon
A
34

In onCreateView create FrameLayout - this is the container for you fragmenView. Then create your R.layout.testone and add it to frameLayout.

In onConfigurationChanged clear FrameLayout, create R.layout.testone again and add it to frameLayout.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
{
    frameLayout = new FrameLayout(getActivity());
    LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = inflater.inflate(R.layout.testone, null);
    frameLayout .addView(view);
    return frameLayout; 
}

@Override
public void onConfigurationChanged(Configuration newConfig) 
{
    super.onConfigurationChanged(newConfig);
    frameLayout. removeAllViews();
    LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = inflater.inflate(R.layout.testone, null);
    frameLayout .addView(view);
}

Now all will work as you want!

Aerosol answered 1/7, 2013 at 11:25 Comment(1)
it have lot of problemObjection

© 2022 - 2024 — McMap. All rights reserved.