Java Code Examples for android.view.View#saveHierarchyState()

The following examples show how to use android.view.View#saveHierarchyState() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
public void saveActionViewStates(Bundle outStates) {
    SparseArray<Parcelable> viewStates = null;

    final int itemCount = size();
    for (int i = 0; i < itemCount; i++) {
        final MenuItem item = getItem(i);
        final View v = item.getActionView();
        if (v != null && v.getId() != View.NO_ID) {
            if (viewStates == null) {
                viewStates = new SparseArray<Parcelable>();
            }
            v.saveHierarchyState(viewStates);
            if (item.isActionViewExpanded()) {
                outStates.putInt(EXPANDED_ACTION_VIEW_ID, item.getItemId());
            }
        }
        if (item.hasSubMenu()) {
            final SubMenuBuilder subMenu = (SubMenuBuilder) item.getSubMenu();
            subMenu.saveActionViewStates(outStates);
        }
    }

    if (viewStates != null) {
        outStates.putSparseParcelableArray(getActionViewStatesKey(), viewStates);
    }
}
 
Example 2
Source Project: simple-stack   File: Backstack.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Provides the means to save the provided view's hierarchy state
 * and its optional StateBundle via {@link Bundleable} into a {@link SavedState}.
 *
 * @param view the view that belongs to a certain key
 */
public void persistViewToState(@Nullable View view) {
    if(view != null) {
        Object key = KeyContextWrapper.getKey(view.getContext());
        if(key == null) {
            throw new IllegalArgumentException("The view [" + view + "] contained no key in its context hierarchy. The view or its parent hierarchy should be inflated by a layout inflater from `stateChange.createContext(baseContext, key)`, or a KeyContextWrapper.");
        }
        SparseArray<Parcelable> viewHierarchyState = new SparseArray<>();
        view.saveHierarchyState(viewHierarchyState);
        StateBundle bundle = null;
        if(view instanceof Bundleable) {
            bundle = ((Bundleable) view).toBundle();
        }
        SavedState previousSavedState = getSavedState(key);
        previousSavedState.setViewHierarchyState(viewHierarchyState);
        previousSavedState.setViewBundle(bundle);
    }
}
 
Example 3
Source Project: TabStacker   File: ViewData.java    License: Apache License 2.0 6 votes vote down vote up
static Bundle saveViewHierarchy(@NonNull View view) {

        Bundle bundle = new Bundle();
        SparseArray<Parcelable> savedViewHierarchy = new SparseArray<>();

        view.saveHierarchyState(savedViewHierarchy);

        int count = savedViewHierarchy.size();
        for(int i=0; i<count; ++i) {
            int key = savedViewHierarchy.keyAt(i);
            Parcelable parcelable = savedViewHierarchy.get(key);
            String bundleKey = "" + key;
            bundle.putParcelable(bundleKey, parcelable);
        }

        return bundle;
    }
 
Example 4
@NonNull
public Bundle createInstanceState() {
  Bundle state = new Bundle();
  if (checkedItem != null) {
    state.putInt(STATE_CHECKED_ITEM, checkedItem.getItemId());
  }
  // Store the states of the action views.
  SparseArray<ParcelableSparseArray> actionViewStates = new SparseArray<>();
  for (int i = 0, size = items.size(); i < size; i++) {
    NavigationMenuItem navigationMenuItem = items.get(i);
    if (navigationMenuItem instanceof NavigationMenuTextItem) {
      MenuItemImpl item = ((NavigationMenuTextItem) navigationMenuItem).getMenuItem();
      View actionView = item != null ? item.getActionView() : null;
      if (actionView != null) {
        ParcelableSparseArray container = new ParcelableSparseArray();
        actionView.saveHierarchyState(container);
        actionViewStates.put(item.getItemId(), container);
      }
    }
  }
  state.putSparseParcelableArray(STATE_ACTION_VIEWS, actionViewStates);
  return state;
}
 
Example 5
@Override
protected Parcelable onSaveInstanceState() {
    for (int i=0; i<getChildCount(); i++) {
        View view = getChildAt(i);
        LayoutParams lp = (LayoutParams) view.getLayoutParams();
        view.saveHierarchyState(mContainer);

        if (view.getId() != NO_ID) {
            SavedState s = new SavedState(mContainer.get(view.getId()));
            s.left = lp.left;
            s.top = lp.top;
            s.movedFlag = lp.moved;
            mContainer.put(view.getId(), s);
        }
    }
    Parcelable p = super.onSaveInstanceState();
    SavedState ss = new SavedState(p);
    ss.container = mContainer;
    return ss;
}
 
Example 6
Source Project: android-apps   File: MenuBuilder.java    License: MIT License 6 votes vote down vote up
public void saveActionViewStates(Bundle outStates) {
    SparseArray<Parcelable> viewStates = null;

    final int itemCount = size();
    for (int i = 0; i < itemCount; i++) {
        final MenuItem item = getItem(i);
        final View v = item.getActionView();
        if (v != null && v.getId() != View.NO_ID) {
            if (viewStates == null) {
                viewStates = new SparseArray<Parcelable>();
            }
            v.saveHierarchyState(viewStates);
            if (item.isActionViewExpanded()) {
                outStates.putInt(EXPANDED_ACTION_VIEW_ID, item.getItemId());
            }
        }
        if (item.hasSubMenu()) {
            final SubMenuBuilder subMenu = (SubMenuBuilder) item.getSubMenu();
            subMenu.saveActionViewStates(outStates);
        }
    }

    if (viewStates != null) {
        outStates.putSparseParcelableArray(getActionViewStatesKey(), viewStates);
    }
}
 
Example 7
Source Project: zen4android   File: MenuBuilder.java    License: MIT License 6 votes vote down vote up
public void saveActionViewStates(Bundle outStates) {
    SparseArray<Parcelable> viewStates = null;

    final int itemCount = size();
    for (int i = 0; i < itemCount; i++) {
        final MenuItem item = getItem(i);
        final View v = item.getActionView();
        if (v != null && v.getId() != View.NO_ID) {
            if (viewStates == null) {
                viewStates = new SparseArray<Parcelable>();
            }
            v.saveHierarchyState(viewStates);
            if (item.isActionViewExpanded()) {
                outStates.putInt(EXPANDED_ACTION_VIEW_ID, item.getItemId());
            }
        }
        if (item.hasSubMenu()) {
            final SubMenuBuilder subMenu = (SubMenuBuilder) item.getSubMenu();
            subMenu.saveActionViewStates(outStates);
        }
    }

    if (viewStates != null) {
        outStates.putSparseParcelableArray(getActionViewStatesKey(), viewStates);
    }
}
 
Example 8
Source Project: zhangshangwuda   File: MenuBuilder.java    License: Apache License 2.0 6 votes vote down vote up
public void saveActionViewStates(Bundle outStates) {
    SparseArray<Parcelable> viewStates = null;

    final int itemCount = size();
    for (int i = 0; i < itemCount; i++) {
        final MenuItem item = getItem(i);
        final View v = item.getActionView();
        if (v != null && v.getId() != View.NO_ID) {
            if (viewStates == null) {
                viewStates = new SparseArray<Parcelable>();
            }
            v.saveHierarchyState(viewStates);
            if (item.isActionViewExpanded()) {
                outStates.putInt(EXPANDED_ACTION_VIEW_ID, item.getItemId());
            }
        }
        if (item.hasSubMenu()) {
            final SubMenuBuilder subMenu = (SubMenuBuilder) item.getSubMenu();
            subMenu.saveActionViewStates(outStates);
        }
    }

    if (viewStates != null) {
        outStates.putSparseParcelableArray(getActionViewStatesKey(), viewStates);
    }
}
 
Example 9
public void saveActionViewStates(Bundle outStates) {
    SparseArray<Parcelable> viewStates = null;

    final int itemCount = size();
    for (int i = 0; i < itemCount; i++) {
        final MenuItem item = getItem(i);
        final View v = item.getActionView();
        if (v != null && v.getId() != View.NO_ID) {
            if (viewStates == null) {
                viewStates = new SparseArray<Parcelable>();
            }
            v.saveHierarchyState(viewStates);
            if (item.isActionViewExpanded()) {
                outStates.putInt(EXPANDED_ACTION_VIEW_ID, item.getItemId());
            }
        }
        if (item.hasSubMenu()) {
            final SubMenuBuilder subMenu = (SubMenuBuilder) item.getSubMenu();
            subMenu.saveActionViewStates(outStates);
        }
    }

    if (viewStates != null) {
        outStates.putSparseParcelableArray(getActionViewStatesKey(), viewStates);
    }
}
 
Example 10
Source Project: flowless   File: FlowlessPagerAdapter.java    License: Apache License 2.0 5 votes vote down vote up
private void saveStateForView(View view, Bundle savedState) {
    if(view instanceof FlowLifecycles.PreSaveViewStateListener) {
        ((FlowLifecycles.PreSaveViewStateListener) view).preSaveViewState();
    }
    SparseArray<Parcelable> viewState = new SparseArray<>();
    view.saveHierarchyState(viewState);
    savedState.putSparseParcelableArray("viewState", viewState);
    if(view instanceof Bundleable) {
        savedState.putBundle("bundle", ((Bundleable) view).toBundle());
    }
}
 
Example 11
Source Project: epoxy   File: ViewHolderState.java    License: Apache License 2.0 5 votes vote down vote up
public void save(View view) {
  int originalId = view.getId();
  setIdIfNoneExists(view);

  view.saveHierarchyState(this);
  view.setId(originalId);
}
 
Example 12
/**
 * Save views regardless what's the current policy is.
 *
 * @param view view to save
 * @param id unique id for the view within this ViewsStateBundle
 */
protected final void saveViewUnchecked(View view, int id) {
    if (mChildStates != null) {
        String key = getSaveStatesKey(id);
        SparseArray<Parcelable> container = new SparseArray<Parcelable>();
        view.saveHierarchyState(container);
        mChildStates.put(key, container);
    }
}
 
Example 13
Source Project: flow   File: State.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Save view hierarchy state so it can be restored later from {@link #restore(View)}.  The view
 * must have a non-zero id.
 */
public void save(@NonNull View view) {
  int viewId = view.getId();
  Preconditions.checkArgument(viewId != 0,
      "Cannot save state for View with no id " + view.getClass().getSimpleName());
  SparseArray<Parcelable> state = new SparseArray<>();
  view.saveHierarchyState(state);
  viewStateById.put(viewId, state);
}
 
Example 14
Source Project: Defrag   File: ViewStack.java    License: Apache License 2.0 4 votes vote down vote up
void saveState(@NonNull View view) {
	final SparseArray<Parcelable> parcelableSparseArray = new SparseArray<>();
	view.saveHierarchyState(parcelableSparseArray);
	viewState = parcelableSparseArray;
}
 
Example 15
private void putInDetached(int position, View view) {
  SparseArray<Parcelable> viewState = new SparseArray<>();
  view.saveHierarchyState(viewState);
  detached.put(position, viewState);
}
 
Example 16
Source Project: flowless   File: State.java    License: Apache License 2.0 4 votes vote down vote up
public void save(@NonNull View view) {
    SparseArray<Parcelable> state = new SparseArray<>();
    view.saveHierarchyState(state);
    viewState = state;
}
 
Example 17
Source Project: fragnums   File: BackstackFrame.java    License: Apache License 2.0 4 votes vote down vote up
public BackstackFrame(Screen screen, View view) {
  this.screen = screen;
  viewState = new SparseArray<>();
  view.saveHierarchyState(viewState);
}
 
Example 18
Source Project: scoop   File: Screen.java    License: Apache License 2.0 4 votes vote down vote up
public void saveViewState(View view) {
    SparseArray<Parcelable> viewState = new SparseArray<Parcelable>();
    view.saveHierarchyState(viewState);
    this.viewState = viewState;
}
 
Example 19
/**
 * The method, which is invoked by a {@link TabSwitcher} to save the current state of a tab. It
 * initializes the view holder pattern, which is provided by the decorator and then delegates
 * the method call to the decorator's custom implementation of the method
 * <code>onSaveInstanceState(...):void</code>.
 *
 * @param view
 *         The view, which is used to visualize the tab, as an instance of the class {@link
 *         View}
 * @param tab
 *         The tab, whose state should be saved, as an instance of the class {@link Tab}. The
 *         tab may not be null
 * @param index
 *         The index of the tab, whose state should be saved, as an {@link Integer} value
 * @return The bundle, which has been used to save the state, as an instance of the class {@link
 * Bundle}. The bundle may not be null
 */
@NonNull
public final Bundle saveInstanceState(@NonNull final View view, @NonNull final Tab tab,
                                      final int index) {
    setCurrentParentView(view);
    int viewType = getViewType(tab, index);
    Bundle outState = new Bundle();
    SparseArray<Parcelable> viewStates = new SparseArray<>();
    view.saveHierarchyState(viewStates);
    outState.putSparseParcelableArray(VIEW_HIERARCHY_STATE_EXTRA, viewStates);
    onSaveInstanceState(view, tab, index, viewType, outState);
    return outState;
}