Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fragments and onConfigurationChanged

I'm trying to do something I do with activities, but within a fragment. What I do is using activities:

First stop the activity restarts when rotating the device android:configChanges="keyboardHidden|orientation|screenSize"

in my activity add:

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

 setContentView(R.layout.main);
}

So get the activity does not restart, but reloading the main.xml, to use the layout-land

Now I have an activity showing viewpager, which contains three fragments. Everything works properly. Detection of the rotation is in the 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 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 to use:

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 different ways, but always without success I can not inflate properly.

Can anyone tell me how I have to do?

Thanks in advance, I appreciate the help

Regards

like image 787
Sergio76 Avatar asked Apr 29 '13 15:04

Sergio76


People also ask

What is the difference between onCreate () and onCreateView () lifecycle methods in fragment?

onCreate is called on initial creation of the fragment. You do your non graphical initializations here. It finishes even before the layout is inflated and the fragment is visible. onCreateView is called to inflate the layout of the fragment i.e graphical initialization usually takes place here.

What is a fragment activity?

A Fragment represents a reusable portion of your app's UI. A fragment defines and manages its own layout, has its own lifecycle, and can handle its own input events. Fragments cannot live on their own--they must be hosted by an activity or another fragment.

What are fragments in Java?

In Android, the fragment is the part of Activity which represents a portion of User Interface(UI) on the screen.

What are retained fragments?

Retained fragments take advantage of the fact that a fragment's view can be destroyed and recreated without having to destroy the fragment itself. During a configuration change, the FragmentManager first destroys the views of the fragments in its list.


1 Answers

If I understand correctly, you don't want to the fragment to reload on each rotation. Instead you want to relayout the views and update them with information you already have.

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 does not. It actually return the parent of the view you returned in onCreateView() which is exactly what you need. getView() will return an instance of NoSaveStateFrameLayout, which is used specifically for Fragments as its root view.

Hope this helps.

like image 65
Aswin Rajendiran Avatar answered Oct 19 '22 07:10

Aswin Rajendiran