Java Code Examples for android.app.Fragment#setInitialSavedState()

The following examples show how to use android.app.Fragment#setInitialSavedState() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExoHelper.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Try refreshing the given Fragment in place, preserving construction args. The new Fragment will
 * be constructed via a fresh class lookup. This method must be called from the UI thread
 *
 * @param fragment the fragment to refresh
 * @param preserveState if true, attempt to save the Fragment's current state and restore it to
 *     the new instance of the fragment
 */
public static void refreshFragment(Fragment fragment, boolean preserveState) {
  assertIsUiThread();
  FragmentManager manager = fragment.getFragmentManager();
  final Bundle args = fragment.getArguments();
  Fragment replacement = createFragment(fragment.getClass().getName(), args);

  String tag = fragment.getTag();

  if (preserveState) {
    // Restore any state that's possible
    final SavedState savedState = manager.saveFragmentInstanceState(fragment);
    replacement.setInitialSavedState(savedState);
  }

  int containerViewId = ((View) fragment.getView().getParent()).getId();
  final FragmentTransaction transaction = manager.beginTransaction();
  transaction.remove(fragment);
  if (tag != null) {
    transaction.add(containerViewId, replacement, tag);
  } else {
    transaction.add(containerViewId, replacement);
  }
  transaction.commit();
}
 
Example 2
Source File: FragmentStatePagerAdapter.java    From PrayTime-Android with Apache License 2.0 5 votes vote down vote up
@SuppressLint("NewApi")
@Override
public Object instantiateItem(ViewGroup container, int position) {
    // If we already have this item instantiated, there is nothing
    // to do.  This can happen when we are restoring the entire pager
    // from its saved state, where the fragment manager has already
    // taken care of restoring the fragments we previously had instantiated.
    if (mFragments.size() > position) {
        Fragment f = mFragments.get(position);
        if (f != null) {
            return f;
        }
    }

    if (mCurTransaction == null) {
        mCurTransaction = mFragmentManager.beginTransaction();
    }

    Fragment fragment = getItem(position);
    if (DEBUG) Log.v(TAG, "Adding item #" + position + ": f=" + fragment);
    if (mSavedState.size() > position) {
        Fragment.SavedState fss = mSavedState.get(position);
        if (fss != null) {
            fragment.setInitialSavedState(fss);
        }
    }
    while (mFragments.size() <= position) {
        mFragments.add(null);
    }
    fragment.setMenuVisibility(false);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
        fragment.setUserVisibleHint(false);
    }
    mFragments.set(position, fragment);
    mCurTransaction.add(container.getId(), fragment);

    return fragment;
}
 
Example 3
Source File: FragmentStatePagerAdapter.java    From MiBandDecompiled with Apache License 2.0 5 votes vote down vote up
public Object instantiateItem(ViewGroup viewgroup, int i)
{
    if (f.size() > i)
    {
        Fragment fragment1 = (Fragment)f.get(i);
        if (fragment1 != null)
        {
            return fragment1;
        }
    }
    if (d == null)
    {
        d = c.beginTransaction();
    }
    Fragment fragment = getItem(i);
    if (e.size() > i)
    {
        android.app.Fragment.SavedState savedstate = (android.app.Fragment.SavedState)e.get(i);
        if (savedstate != null)
        {
            fragment.setInitialSavedState(savedstate);
        }
    }
    for (; f.size() <= i; f.add(null)) { }
    FragmentCompat.setMenuVisibility(fragment, false);
    FragmentCompat.setUserVisibleHint(fragment, false);
    f.set(i, fragment);
    d.add(viewgroup.getId(), fragment);
    return fragment;
}
 
Example 4
Source File: FragmentStatePagerAdapter.java    From BlackLight with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Object instantiateItem(ViewGroup container, int position) {
    // If we already have this item instantiated, there is nothing
    // to do.  This can happen when we are restoring the entire pager
    // from its saved state, where the fragment manager has already
    // taken care of restoring the fragments we previously had instantiated.
    if (mFragments.size() > position) {
        Fragment f = mFragments.get(position);
        if (f != null) {
            return f;
        }
    }

    if (mCurTransaction == null) {
        mCurTransaction = mFragmentManager.beginTransaction();
    }

    Fragment fragment = getItem(position);
    if (DEBUG) Log.v(TAG, "Adding item #" + position + ": f=" + fragment);
    if (mSavedState.size() > position) {
        Fragment.SavedState fss = mSavedState.get(position);
        if (fss != null) {
            fragment.setInitialSavedState(fss);
        }
    }
    while (mFragments.size() <= position) {
        mFragments.add(null);
    }
    fragment.setMenuVisibility(false);
    fragment.setUserVisibleHint(false);
    mFragments.set(position, fragment);
    mCurTransaction.add(container.getId(), fragment);

    return fragment;
}
 
Example 5
Source File: FragmentStatePagerAdapter.java    From V.FlyoutTest with MIT License 5 votes vote down vote up
@Override
public Object instantiateItem(ViewGroup container, int position) {
    // If we already have this item instantiated, there is nothing
    // to do.  This can happen when we are restoring the entire pager
    // from its saved state, where the fragment manager has already
    // taken care of restoring the fragments we previously had instantiated.
    if (mFragments.size() > position) {
        Fragment f = mFragments.get(position);
        if (f != null) {
            return f;
        }
    }

    if (mCurTransaction == null) {
        mCurTransaction = mFragmentManager.beginTransaction();
    }

    Fragment fragment = getItem(position);
    if (DEBUG) Log.v(TAG, "Adding item #" + position + ": f=" + fragment);
    if (mSavedState.size() > position) {
        Fragment.SavedState fss = mSavedState.get(position);
        if (fss != null) {
            fragment.setInitialSavedState(fss);
        }
    }
    while (mFragments.size() <= position) {
        mFragments.add(null);
    }
    FragmentCompat.setMenuVisibility(fragment, false);
    FragmentCompat.setUserVisibleHint(fragment, false);
    mFragments.set(position, fragment);
    mCurTransaction.add(container.getId(), fragment);

    return fragment;
}
 
Example 6
Source File: TapchatFragmentStatePagerAdapter.java    From tapchat-android with Apache License 2.0 4 votes vote down vote up
@Override public Object instantiateItem(ViewGroup container, int position) {
    // If we already have this item instantiated, there is nothing
    // to do.  This can happen when we are restoring the entire pager
    // from its saved state, where the fragment manager has already
    // taken care of restoring the fragments we previously had instantiated.
    if (mFragments.size() > position) {
        Fragment f = mFragments.get(position);
        if (f != null) {
            int newPos = getItemPosition(f);
            if (position == newPos) {
                if (f.getView() != null && f.getView().getParent() != container) {
                    ((ViewGroup) f.getView().getParent()).removeView(f.getView());
                    container.addView(f.getView());
                }
                return f;
            } else {
                mFragments.remove(position);
                mFragments.ensureCapacity(newPos + 1);
                for (int i = mFragments.size(); i < (newPos +1); i++) {
                    mFragments.add(null);
                }
                mFragments.set(newPos, f);
            }
        }
    }

    if (mCurTransaction == null) {
        mCurTransaction = mFragmentManager.beginTransaction();
    }

    Fragment fragment = getItem(position);
    if (DEBUG) Log.v(TAG, "Adding item #" + position + ": f=" + fragment);
    if (mSavedState.size() > position) {
        Fragment.SavedState fss = mSavedState.get(position);
        if (fss != null) {
            fragment.setInitialSavedState(fss);
        }
    }
    while (mFragments.size() <= position) {
        mFragments.add(null);
    }
    fragment.setMenuVisibility(false);
    mFragments.set(position, fragment);
    mCurTransaction.add(container.getId(), fragment, getTag(position));

    return fragment;
}