title: Serializable ViewPager in Fragment causes Android App crash when open the app from background
body: <p>I try to pass a ViewPage to Fragement in my app. But the app crashes when open app after it was killed by OS, or reopen the App from background while "Don't keep activities" is enabled at developer options.</p>
  
  <p>Here is summary of the error message from Logcat:<br><br>
  (1) java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test3/com.test.MainActivity}: java.lang.RuntimeException: Parcelable encountered IOException reading a Serializable object (name = com.test.VerticalViewPager)<br><br>
  (2) Caused by: java.lang.RuntimeException: Parcelable encountered IOException reading a Serializable object (name = com.test.VerticalViewPager)<br><br>
  (3) Caused by: java.io.InvalidClassException: com.test.VerticalViewPager; no valid constructor<br><br></p>
  
  <p>After some reseraches, I found that Java class which implemented Serializable MUST has a no-arg constructor (Reference: <a href="http://burnignorance.com/java-web-development-tips/java-serialization-and-need-for-a-no-arg-constructor/" rel="nofollow noreferrer">http://burnignorance.com/java-web-development-tips/java-serialization-and-need-for-a-no-arg-constructor/</a>).
  Android Studio keeps warning me that "There is no default constructor available in 'android.support.v4.view.ViewPager'" when I try to add a no-arg constructor to my ViewPager class.</p>
  
  <p>I did a whole day research and still have no idea of how to solve it. Please let me know if you have any idea, thanks<br><br><br></p>
  
  <p>Here is the Fragment class:</p>
  
  <pre><code>public final class FragmentTest extends Fragment {
  
      private static final String VERTICALVIEWPAGER = "VERTICALVIEWPAGER";
  
      public static FragmentTest newInstance(VerticalViewPager myVerticalViewPager) {
          FragmentTest f = new FragmentTest();
          Bundle args = new Bundle();
          args.putSerializable(VERTICALVIEWPAGER, myVerticalViewPager);
          f.setArguments(args);
          return f;
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          context = (MainActivity) getActivity();
  
          // Error at this line when open the App after it was killed by OS (Or reopen the App from background while "Don't keep activities" is enabled )
          myVerticalViewPager = getArguments() != null ? (VerticalViewPager) (getArguments().getSerializable(VERTICALVIEWPAGER)) : null;
      }
  
  }
  </code></pre>
  
  <p><br>
  Here is the ViewPager class:</p>
  
  <pre><code>public final class VerticalViewPager extends ViewPager implements Serializable{
  
      public VerticalViewPager(Context context) {
          super(context);
          init();
      }
  
      public VerticalViewPager(Context context, AttributeSet attrs) {
          super(context, attrs);
          init();
      }
  
      private void init() {
          // The majority of the magic happens here
          setPageTransformer(true, new VerticalPageTransformer());
          // The easiest way to get rid of the overscroll drawing that happens on the left and right
          setOverScrollMode(OVER_SCROLL_NEVER);
      }
  
      .
      .
      .
  }
  </code></pre>
  
  <p><br>Here is full Logcat message:<br></p>
  
  <blockquote>
    <p>2019-04-16 11:40:27.676 5369-5369/com.test3 E/AndroidRuntime: FATAL
    EXCEPTION: main Process: com.test3, PID: 5369
    java.lang.RuntimeException: Unable to start activity
    ComponentInfo{com.test3/com.test.MainActivity}:
    java.lang.RuntimeException: Parcelable encountered IOException reading
    a Serializable object (name = com.test.VerticalViewPager)     at
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2812)
      at
    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2890)
      at android.app.ActivityThread.-wrap11(Unknown Source:0)     at
    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1611)
      at android.os.Handler.dispatchMessage(Handler.java:106)     at
    android.os.Looper.loop(Looper.java:171)   at
    android.app.ActivityThread.main(ActivityThread.java:6633)     at
    java.lang.reflect.Method.invoke(Native Method)    at
    com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823) 
    Caused by: java.lang.RuntimeException: Parcelable encountered
    IOException reading a Serializable object (name =
    com.test.VerticalViewPager)   at
    android.os.Parcel.readSerializable(Parcel.java:3007)  at
    android.os.Parcel.readValue(Parcel.java:2797)     at
    android.os.Parcel.readArrayMapInternal(Parcel.java:3115)  at
    android.os.BaseBundle.initializeFromParcelLocked(BaseBundle.java:273)
      at android.os.BaseBundle.unparcel(BaseBundle.java:226)  at
    android.os.BaseBundle.getSerializable(BaseBundle.java:1199)   at
    android.os.Bundle.getSerializable(Bundle.java:1033)   at
    com.test.view.FragmentTest.onCreate(FragmentTest.java:79)     at
    android.support.v4.app.Fragment.performCreate(Fragment.java:2339)     at
    android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1377)
      at
    android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
      at
    android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
      at
    android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3217)
      at
    android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:3166)
      at
    android.support.v4.app.FragmentController.dispatchCreate(FragmentController.java:181)
      at
    android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:319)
      at
    android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:84)
      at com.test.MainActivity.onCreate(MainActivity.java:364)    at
    android.app.Activity.performCreate(Activity.java:7084)    at
    android.app.Activity.performCreate(Activity.java:7075)    at
    android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)
      at
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2765)
      at
    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2890) 
      at android.app.ActivityThread.-wrap11(Unknown Source:0)     at
    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1611) 
      at android.os.Handler.dispatchMessage(Handler.java:106)     at
    android.os.Looper.loop(Looper.java:171)   at
    android.app.ActivityThread.main(ActivityThread.java:6633)     at
    java.lang.reflect.Method.invoke(Native Method)    at
    com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)  
    Caused by: java.io.InvalidClassException: com.test.VerticalViewPager;
    no valid constructor  at
    java.io.ObjectStreamClass$ExceptionInfo.newInvalidClassException(ObjectStreamClass.java:147)
      at
    java.io.ObjectStreamClass.checkDeserialize(ObjectStreamClass.java:757)
      at
    java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1778)
      at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1354)
      at java.io.ObjectInputStream.readObject(ObjectInputStream.java:374)
      at android.os.Parcel.readSerializable(Parcel.java:3005)     at
    android.os.Parcel.readValue(Parcel.java:2797)     at
    android.os.Parcel.readArrayMapInternal(Parcel.java:3115)      at
    android.os.BaseBundle.initializeFromParcelLocked(BaseBundle.java:273) 
      at android.os.BaseBundle.unparcel(BaseBundle.java:226)      at
    android.os.BaseBundle.getSerializable(BaseBundle.java:1199)   at
    android.os.Bundle.getSerializable(Bundle.java:1033)   at
    com.test.view.FragmentTest.onCreate(FragmentTest.java:79)     at
    android.support.v4.app.Fragment.performCreate(Fragment.java:2339)     at
    android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1377) 
      at
    android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740) 
      at
    android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809) 
      at
    android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3217) 
      at
    android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:3166) 
      at
    android.support.v4.app.FragmentController.dispatchCreate(FragmentController.java:181) 
      at
    android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:319) 
      at
    android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:84) 
      at com.test.MainActivity.onCreate(MainActivity.java:364)    at
    android.app.Activity.performCreate(Activity.java:7084)    at
    android.app.Activity.performCreate(Activity.java:7075)    at
    android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215) 
      at
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2765) 
      at
    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2890) 
      at android.app.ActivityThread.-wrap11(Unknown Source:0)     at
    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1611) 
      at android.os.Handler.dispatchMessage(Handler.java:106)     at
    android.os.Looper.loop(Looper.java:171)   at
    android.app.ActivityThread.main(ActivityThread.java:6633)     at
    java.lang.reflect.Method.invoke(Native Method)    at
    com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)</p>
  </blockquote>