content: /*
   * Created by LuaView.
   * Copyright (c) 2017, Alibaba Group. All rights reserved.
   *
   * This source code is licensed under the MIT.
   * For the full copyright and license information,please view the LICENSE file in the root directory of this source tree.
   */
  
  package com.taobao.luaview.view.adapter;
  
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentStatePagerAdapter;
  import android.util.Log;
  import android.view.ViewGroup;
  
  import java.lang.reflect.Field;
  
  /**
   * 解决bug
   * android.os.BadParcelableException: ClassNotFoundException when unmarshalling: android.support.v4.app.FragmentManagerState
   * http://stackoverflow.com/questions/11381470/classnotfoundexception-when-unmarshalling-android-support-v4-view-viewpagersav
   * @author song
   */
  public abstract class FixedFragmentStatePagerAdapter extends FragmentStatePagerAdapter {
  
      public FixedFragmentStatePagerAdapter(FragmentManager fm) {
          super(fm);
      }
  
      @Override
      public Object instantiateItem(ViewGroup container, int position) {
          final Object fragment = super.instantiateItem(container, position);
          try {
              final Field saveFragmentStateField = Fragment.class.getDeclaredField("mSavedFragmentState");
              saveFragmentStateField.setAccessible(true);
              final Bundle savedFragmentState = (Bundle) saveFragmentStateField.get(fragment);
              if (savedFragmentState != null) {
                  savedFragmentState.setClassLoader(Fragment.class.getClassLoader());
              }
          } catch (Exception e) {
              Log.w("FragmentPagerAdapter", "Could not get mSavedFragmentState field: " + e);
          }
          return fragment;
      }
  }