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

The following examples show how to use android.view.View#saveHierarchyState() . 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: NavigationMenuPresenter.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
@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 2
Source File: MenuBuilder.java    From Libraries-for-Android-Developers with 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 3
Source File: MenuBuilder.java    From zhangshangwuda with 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 4
Source File: MenuBuilder.java    From zen4android with 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 5
Source File: MenuBuilder.java    From android-apps with 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 6
Source File: RearrangeableLayout.java    From RearrangeableLayout with MIT License 6 votes vote down vote up
@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 7
Source File: MenuBuilder.java    From CSipSimple with GNU General Public License v3.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 8
Source File: ViewData.java    From TabStacker with 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 9
Source File: Backstack.java    From simple-stack with 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 10
Source File: ViewHolderState.java    From epoxy with 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 11
Source File: FlowlessPagerAdapter.java    From flowless with 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 12
Source File: ViewsStateBundle.java    From adt-leanback-support with Apache License 2.0 5 votes vote down vote up
/**
 * 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 File: State.java    From flow with 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 File: State.java    From flowless with 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 15
Source File: BackstackFrame.java    From fragnums with 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 16
Source File: ViewStatePagerAdapter.java    From ViewStatePagerAdapter with Apache License 2.0 4 votes vote down vote up
private void putInDetached(int position, View view) {
  SparseArray<Parcelable> viewState = new SparseArray<>();
  view.saveHierarchyState(viewState);
  detached.put(position, viewState);
}
 
Example 17
Source File: Screen.java    From scoop with 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 18
Source File: ViewStack.java    From Defrag with 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 19
Source File: TabSwitcherDecorator.java    From ChromeLikeTabSwitcher with Apache License 2.0 3 votes vote down vote up
/**
 * 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;
}