Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragments and Orientation change

What is the correct way to handle an orientation change when using Fragments?

I have a landscape layout that contains 2 fragments (instantiated in code into FrameLayouts). When I switch to portrait mode (the layout of which contains only one FrameLayout that holds the left pane only), the right hand fragment is no longer required.

I am receiving an error:

E/AndroidRuntime(4519): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f060085 for fragment myFragment{418a2200 #2 id=0x7f060085} 

which is assume is my activity trying to re-attach the fragment where it was before the orientation change but as the view that contains the fragment does not exist in portrait mode the error is thrown.

I have tried the following hide/remove/detach methods but still get the error. What is the correct way to tell a fragment it is not needed any more and do not try to display?

@Override public void onCreate(Bundle b) {     super.onCreate(b);     Fragment f = getSupportFragmentManager().findFragmentById(R.id.fragholder2);      //rightPane is a framelayout that holds my fragment.     if (rightPane == null && f != null) {          FragmentTransaction ft = getSupportFragmentManager().beginTransaction();          ft.hide(f);     // This doesnt work          ft.remove(f);   // neither does this          ft.detach(f);   // or this          ft.commit;     } } 
like image 815
Kuffs Avatar asked Apr 02 '12 11:04

Kuffs


People also ask

How do I change the orientation of a fragment?

Use the following code line in the fragment where you want a specific (in this case portrait) orientation. getActivity(). setRequestedOrientation( ActivityInfo. SCREEN_ORIENTATION_PORTRAIT);

What happens when screen orientation changes in Android?

When you rotate your device and the screen changes orientation, Android usually destroys your application's existing Activities and Fragments and recreates them. Android does this so that your application can reload resources based on the new configuration.

How do I open the fragment in landscape mode?

setRequestedOrientation(ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE); The fragment does load in landscape mode.

What is the fragment life cycle in Android?

A fragment can be used in multiple activities. Fragment life cycle is closely related to the life cycle of its host activity which means when the activity is paused, all the fragments available in the activity will also be stopped. A fragment can implement a behaviour that has no user interface component.


1 Answers

I ran into the same problem and I think I figured out another solution. This solution may be better because you don't have to add the fragment to the back stack.

Remove the right hand side fragment from your activity in Activity.onSaveInstanceState() before calling super.onSaveInstanceState(). This works for me:

public MyActivity extends Activity {        @Override     public onCreate(Bundle state)     {         super.onCreate(state);          // Set content view         setContentView(R.layout.my_activity);          // Store whether this is a dual pane layout         mDualPane = findViewById(R.id.rightFragHolder) != null;          // Other stuff, populate the left fragment, etc.         .         .         .         if (mDualPane)         {             mRightFragment = new RightFragment();             FragmentManager fm = getFragmentManager();             FragmentTransaction ft = fm.beginTransaction();             ft.replace(R.id.rightFragHolder, mRightFragment);             ft.commit()         }     }       @Override     public void onSaveInstanceState(Bundle state)     {         if (mDualPane)         {             FragmentManager fm = getFragmentManager();             FragmentTransaction ft = fm.beginTransaction();             ft.remove(mRightFragment);             ft.commit()         }          super.onSaveInstanceState(state);     }       private boolean mDualPane;     private Fragment mRightFragment; } 
like image 138
Mukul M. Avatar answered Sep 28 '22 04:09

Mukul M.