1

I have several fragments in activity, changed by bottombar. I tried to implement fragment state handling by using Once for all, how to correctly save instance state of Fragments in back stack? The problem is that

    mContent = getSupportFragmentManager().getFragment(savedInstanceState, TAG);

Returns the next error:

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference

While debugging I have the next savedInstanceSet:

ArrayMap@5665, size = 4
 value[0]=Bundle
 value[1]="FragmentName"
 value[2]=FragmentManagerState
 value[3]={Integer}"0"

Please dont paste solutions like

 setRetainInstance(true),
 android:configChanges="orientation|screenSize"

and so on, because I need to recreate fragment (different xml files for land and portrait).

2 Answers2

1

Use below code;

Add this line to menifest

android:configChanges="orientation|screenSize"

and add this function to your activity file,

 @Override
   protected void onRestoreInstanceState(Bundle savedInstanceState) {
   super.onRestoreInstanceState(savedInstanceState);
          onCreate(savedInstanceState);
       }
Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57
0

Include this in your manifest if you dont want to override config change

<activity android:name=".MainActivity" android:configChanges="orientation|screenSize|screenLayout" >

This basically allows OS to handle orientation change for you if you dont want override onConfigChange this code should work fine

akshay_shahane
  • 4,423
  • 2
  • 17
  • 30