Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using onConfigurationChanged in a fragment

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

like image 624
Sergio76 Avatar asked Jun 14 '13 20:06

Sergio76


People also ask

What are the methods you can override in the fragment class?

The Fragment class has two callback methods, onAttach() and onDetach() , that you can override to perform work when either of these events occur.

What does the onCreateView () method return if a fragment doesn't have any UI?

These files contain only the onCreateView() method to inflate the UI of the fragment and returns the root of the fragment layout. If the fragment does not have any UI, it will return null.

How do I start an intent from a fragment?

If you want to start a new instance of mFragmentFavorite , you can do so via an Intent . Intent intent = new Intent(this, mFragmentFavorite. class); startActivity(intent);

What are retained fragments?

A Fragment represents a reusable portion of your app's User Interface. Retained Fragment consists of the configuration change that causes the underlying Activity to be destroyed. The term "retained" refers to the fragment that will not be destroyed on configuration changes.


1 Answers

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!

like image 161
Nik Avatar answered Sep 20 '22 07:09

Nik