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
FragmentActivity
– ElectroacousticsonConfigurationChanged
you simply inflate a layout and assign it to one of the fields of theActivity
. I would advise you to usesetRetainInstance(true)
instead of theonConfigurationChanged
way. As a manual solution I guess you could always remove all views fromgetView()
and reattach the newly inflated layout. – MckenziemckeononConfigurationChanged
you would setsetRetainInstance()
in theonCreate()
method of theFragment
. 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 withsetRetainInstance()
to keep the data and let the fragment above be recreated(getting the data from that non UI fragment). – Mckenziemckeon