Java Code Examples for android.support.v4.app.fragment#setInitialSavedState()

The following examples show how to use android.support.v4.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: MaterialDemoActivity.java    From RecyclerViewPager with Apache License 2.0 6 votes vote down vote up
@Override
public Fragment getItem(int position, Fragment.SavedState savedState) {
    Fragment f = mFragmentCache.containsKey(position) ? mFragmentCache.get(position)
            : new CheeseListFragment();
    Log.e("test", "getItem:" + position + " from cache" + mFragmentCache.containsKey
            (position));
    if (savedState == null || f.getArguments() == null) {
        Bundle bundle = new Bundle();
        bundle.putInt("index", position);
        f.setArguments(bundle);
        Log.e("test", "setArguments:" + position);
    } else if (!mFragmentCache.containsKey(position)) {
        f.setInitialSavedState(savedState);
        Log.e("test", "setInitialSavedState:" + position);
    }
    mFragmentCache.put(position, f);
    return f;
}
 
Example 2
Source File: MaterialDemoWithLoopPagerActivity.java    From RecyclerViewPager with Apache License 2.0 6 votes vote down vote up
@Override
public Fragment getItem(int position, Fragment.SavedState savedState) {
    position = mRecyclerView.transformToActualPosition(position);
    Fragment f = mFragmentCache.containsKey(position) ? mFragmentCache.get(position)
            : new CheeseListFragment();
    Log.e("test", "getItem:" + position + " from cache" + mFragmentCache.containsKey
            (position));
    if (savedState == null || f.getArguments() == null) {
        Bundle bundle = new Bundle();
        bundle.putInt("index", position);
        f.setArguments(bundle);
        Log.e("test", "setArguments:" + position);
    } else if (!mFragmentCache.containsKey(position)) {
        f.setInitialSavedState(savedState);
        Log.e("test", "setInitialSavedState:" + position);
    }
    mFragmentCache.put(position, f);
    return f;
}
 
Example 3
Source File: FragmentStatePagerAdapterFix.java    From UMS-Interface with GNU General Public License v3.0 5 votes vote down vote up
@SuppressLint("LongLogTag")
@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 4
Source File: TaggedFragmentStatePagerAdapter.java    From oneHookLibraryAndroid with Apache License 2.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);
    mFragments.set(position, fragment);
    mCurTransaction.add(container.getId(), fragment, getTag(position));

    return fragment;
}
 
Example 5
Source File: TaggedFragmentStatePagerAdapter.java    From yahnac with Apache License 2.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, getTag(position));

    return fragment;
}
 
Example 6
Source File: FragmentNoMenuStatePagerAdapter.java    From actor-platform with GNU Affero 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 7
Source File: BackStackEntry.java    From android-multibackstack with Apache License 2.0 5 votes vote down vote up
@NonNull
public Fragment toFragment(@NonNull Context context) {
  Fragment f = Fragment.instantiate(context, fname);
  f.setInitialSavedState(state);
  f.setArguments(args);
  return f;
}
 
Example 8
Source File: ArrayFragmentStatePagerAdapter.java    From ArrayPagerAdapter with Apache License 2.0 5 votes vote down vote up
@SuppressLint("CommitTransaction")
@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 super.instantiateItem(container, position);
        }
    }

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

    Fragment fragment = getFragment(getItem(position), position);
    if (DEBUG) Log.d(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 super.instantiateItem(container, position);
}
 
Example 9
Source File: FragmentListPageAdapter.java    From SprintNBA with Apache License 2.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 10
Source File: FragmentStatePagerAdapter.java    From fragmentviewpager with Apache License 2.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.
    // -----------------------------------------------------------------------------------------

    Fragment existingFragment = getFragment(position);
    if (existingFragment != null) {
        return existingFragment;
    }

    // -----------------------------------------------------------------------------------------
    // Else, continue with normal flow.
    // -----------------------------------------------------------------------------------------

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

    Fragment fragment = instantiateFragment(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 11
Source File: UpdatableFragmentPagerAdapter.java    From UpdatableFragmentStatePagerAdapter with Apache License 2.0 5 votes vote down vote up
@Override
@NonNull
public Object instantiateItem(ViewGroup container, int position) {
    long tag = getItemId(position);
    Fragment fragment = mFragments.get(tag);
    // 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 (fragment != null) {
        return fragment;
    }

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

    fragment = getItem(position);
    // restore state
    final Fragment.SavedState savedState = mSavedStates.get(tag);
    if (savedState != null) {
        fragment.setInitialSavedState(savedState);
    }
    fragment.setMenuVisibility(false);
    fragment.setUserVisibleHint(false);
    mFragments.put(tag, fragment);
    mCurTransaction.add(container.getId(), fragment, "f" + tag);

    return fragment;
}
 
Example 12
Source File: CustomFragmentStatePagerAdapter.java    From RetroMusicPlayer 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 13
Source File: FragmentStatePagerAdapterFix.java    From UMS-Interface with GNU General Public License v3.0 5 votes vote down vote up
@SuppressLint("LongLogTag")
@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 14
Source File: CustomFragmentStatePagerAdapter.java    From Orin 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 15
Source File: UpdatableFragmentPagerAdapter.java    From Dainty with Apache License 2.0 5 votes vote down vote up
@Override
@NonNull
public Object instantiateItem(ViewGroup container, int position) {
    long tag = getItemId(position);
    Fragment fragment = mFragments.get(tag);
    // 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 (fragment != null) {
        return fragment;
    }

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

    fragment = getItem(position);
    // restore state
    final Fragment.SavedState savedState = mSavedStates.get(tag);
    if (savedState != null) {
        fragment.setInitialSavedState(savedState);
    }
    fragment.setMenuVisibility(false);
    fragment.setUserVisibleHint(false);
    mFragments.put(tag, fragment);
    mCurTransaction.add(container.getId(), fragment, "f" + tag);

    return fragment;
}
 
Example 16
Source File: OpenPagerAdapter.java    From OpenPagerAdapter 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 (mItemInfos.size() > position) {
        ItemInfo ii = mItemInfos.get(position);
        if (ii != null) {
            //判断位置是否相等,如果不相等说明新数据有增加或删除(导致了ViewPager那边有空位),
            // 而这时notifyDataSetChanged方法还没有完成,ViewPager会先调用instantiateItem来获取新的页面
            //所以为了不取错页面,我们需要对缓存进行检查和调整位置:checkProcessCacheChanged
            if (ii.position == position) {
                return ii;
            } else {
                checkProcessCacheChanged();
            }
        }
    }

    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 (mItemInfos.size() <= position) {
        mItemInfos.add(null);
    }
    fragment.setMenuVisibility(false);
    fragment.setUserVisibleHint(false);
    ItemInfo<T> iiNew = new ItemInfo<>(fragment, getItemData(position), position);
    mItemInfos.set(position, iiNew);
    mCurTransaction.add(container.getId(), fragment);

    return iiNew;
}
 
Example 17
Source File: StateRestoreDemoActivity.java    From HelloActivityAndFragment with Apache License 2.0 4 votes vote down vote up
private void restoreFragmentState(int index, Fragment fragment) {
    Fragment.SavedState savedState = savedStateSparseArray.get(index);
    fragment.setInitialSavedState(savedState);
}