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

The following examples show how to use android.view.View#restoreHierarchyState() . 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: VerticalStepperView.java    From android-vertical-stepper-view with MIT License 6 votes vote down vote up
@Override
public void onRestoreInstanceState( Parcelable state ) {
    if ( state instanceof Bundle ) {
        Bundle bundle = (Bundle) state;
        super.onRestoreInstanceState( bundle.getParcelable( "super" ) );

        ArrayList<Bundle> containers = bundle
                        .getParcelableArrayList( "containers" );
        SparseArray<View> contentViews = getAdapter().getContentViews();

        for ( Bundle contentViewBundle : containers ) {
            int id = contentViewBundle.getInt( "id" );
            SparseArray<Parcelable> container = contentViewBundle
                            .getSparseParcelableArray( "container" );

            View contentView = contentViews.get( id );
            if ( contentView != null ) {
                contentView.restoreHierarchyState( container );
            }
        }

        getAdapter().jumpTo( bundle.getInt( "focus" ) );
    } else
        super.onRestoreInstanceState( state );
}
 
Example 2
Source File: ViewData.java    From TabStacker with Apache License 2.0 6 votes vote down vote up
static void restoreView(Bundle bundle, @NonNull View view) {

        if (bundle == null) {
            return;
        }

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

        for(String bundleKey : bundle.keySet()) {
            Parcelable parcelable = bundle.getParcelable(bundleKey);
            int key = Integer.parseInt(bundleKey);
            savedViewHierarchy.put(key, parcelable);
        }

        view.restoreHierarchyState(savedViewHierarchy);
    }
 
Example 3
Source File: ViewStatePagerAdapter.java    From ViewStatePagerAdapter with Apache License 2.0 6 votes vote down vote up
@Override public final Object instantiateItem(ViewGroup container, int position) {
  if (detached == null) {
    detached = new SparseArray<>();
  }
  View view = createView(container, position);
  if (view == null) {
    throw new NullPointerException(
        "createView must not return null. (position: " + position + ")");
  }
  SparseArray<Parcelable> viewState = detached.get(position);
  if (viewState != null) {
    view.restoreHierarchyState(viewState);
  }
  container.addView(view);
  attached.put(position, view);
  return view;
}
 
Example 4
Source File: FlowlessPagerAdapter.java    From flowless with Apache License 2.0 6 votes vote down vote up
@Override
public View instantiateItem(ViewGroup container, int position) {
    View view = getItem(position);
    if(bundles[position] != null) {
        Bundle savedState = bundles[position];
        view.restoreHierarchyState(savedState.getSparseParcelableArray("viewState"));
        if(view instanceof Bundleable) {
            ((Bundleable) view).fromBundle(savedState.getBundle("bundle"));
        }
    }
    if(view instanceof FlowLifecycles.ViewLifecycleListener) {
        ((FlowLifecycles.ViewLifecycleListener) view).onViewRestored();
    }
    container.addView(view);
    views[position] = view;
    return view;
}
 
Example 5
Source File: TestUtilsActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/**
 * Restores the saved hierarchy state.
 *
 * @param container The saved hierarchy state.
 */
public static ViewAction restoreHierarchyState(final SparseArray<Parcelable> container) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
      return isAssignableFrom(View.class);
    }

    @Override
    public String getDescription() {
      return "restore the saved state";
    }

    @Override
    public void perform(UiController uiController, View view) {
      uiController.loopMainThreadUntilIdle();
      view.restoreHierarchyState(container);
      uiController.loopMainThreadUntilIdle();
    }
  };
}
 
Example 6
Source File: MenuBuilder.java    From Libraries-for-Android-Developers with MIT License 5 votes vote down vote up
public void restoreActionViewStates(Bundle states) {
    if (states == null) {
        return;
    }

    SparseArray<Parcelable> viewStates = states.getSparseParcelableArray(
            getActionViewStatesKey());

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB && viewStates == null) {
        //Fixes Issue #652 with sdk <= 2.3.6
        return;
    }

    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) {
            v.restoreHierarchyState(viewStates);
        }
        if (item.hasSubMenu()) {
            final SubMenuBuilder subMenu = (SubMenuBuilder) item.getSubMenu();
            subMenu.restoreActionViewStates(states);
        }
    }

    final int expandedId = states.getInt(EXPANDED_ACTION_VIEW_ID);
    if (expandedId > 0) {
        MenuItem itemToExpand = findItem(expandedId);
        if (itemToExpand != null) {
            itemToExpand.expandActionView();
        }
    }
}
 
Example 7
Source File: MenuBuilder.java    From zhangshangwuda with Apache License 2.0 5 votes vote down vote up
public void restoreActionViewStates(Bundle states) {
    if (states == null) {
        return;
    }

    SparseArray<Parcelable> viewStates = states.getSparseParcelableArray(
            getActionViewStatesKey());

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB && viewStates == null) {
        //Fixes Issue #652 with sdk <= 2.3.6
        return;
    }

    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) {
            v.restoreHierarchyState(viewStates);
        }
        if (item.hasSubMenu()) {
            final SubMenuBuilder subMenu = (SubMenuBuilder) item.getSubMenu();
            subMenu.restoreActionViewStates(states);
        }
    }

    final int expandedId = states.getInt(EXPANDED_ACTION_VIEW_ID);
    if (expandedId > 0) {
        MenuItem itemToExpand = findItem(expandedId);
        if (itemToExpand != null) {
            itemToExpand.expandActionView();
        }
    }
}
 
Example 8
Source File: MenuBuilder.java    From zen4android with MIT License 5 votes vote down vote up
public void restoreActionViewStates(Bundle states) {
    if (states == null) {
        return;
    }

    SparseArray<Parcelable> viewStates = states.getSparseParcelableArray(
            getActionViewStatesKey());

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB && viewStates == null) {
        //Fixes Issue #652 with sdk <= 2.3.6
        return;
    }

    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) {
            v.restoreHierarchyState(viewStates);
        }
        if (item.hasSubMenu()) {
            final SubMenuBuilder subMenu = (SubMenuBuilder) item.getSubMenu();
            subMenu.restoreActionViewStates(states);
        }
    }

    final int expandedId = states.getInt(EXPANDED_ACTION_VIEW_ID);
    if (expandedId > 0) {
        MenuItem itemToExpand = findItem(expandedId);
        if (itemToExpand != null) {
            itemToExpand.expandActionView();
        }
    }
}
 
Example 9
Source File: MenuBuilder.java    From android-apps with MIT License 5 votes vote down vote up
public void restoreActionViewStates(Bundle states) {
    if (states == null) {
        return;
    }

    SparseArray<Parcelable> viewStates = states.getSparseParcelableArray(
            getActionViewStatesKey());

    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) {
            v.restoreHierarchyState(viewStates);
        }
        if (item.hasSubMenu()) {
            final SubMenuBuilder subMenu = (SubMenuBuilder) item.getSubMenu();
            subMenu.restoreActionViewStates(states);
        }
    }

    final int expandedId = states.getInt(EXPANDED_ACTION_VIEW_ID);
    if (expandedId > 0) {
        MenuItem itemToExpand = findItem(expandedId);
        if (itemToExpand != null) {
            itemToExpand.expandActionView();
        }
    }
}
 
Example 10
Source File: ViewsStateBundle.java    From adt-leanback-support with Apache License 2.0 5 votes vote down vote up
/**
 * Load view from states, it's none operation if the there is no state associated with the id.
 *
 * @param view view where loads into
 * @param id unique id for the view within this ViewsStateBundle
 */
public final void loadView(View view, int id) {
    if (mChildStates != null) {
        String key = getSaveStatesKey(id);
        SparseArray<Parcelable> container = mChildStates.get(key);
        if (container != null) {
            view.restoreHierarchyState(container);
        }
    }
}
 
Example 11
Source File: ViewHolderState.java    From epoxy with Apache License 2.0 5 votes vote down vote up
public void restore(View view) {
  int originalId = view.getId();
  setIdIfNoneExists(view);

  view.restoreHierarchyState(this);
  view.setId(originalId);
}
 
Example 12
Source File: Backstack.java    From simple-stack with Apache License 2.0 5 votes vote down vote up
/**
 * Restores the state of the view based on the currently stored {@link SavedState}, according to the view's key.
 *
 * @param view the view that belongs to a certain key
 */
public void restoreViewFromState(@Nonnull View view) {
    if(view == null) {
        throw new IllegalArgumentException("You cannot restore state into null view!");
    }
    Object newKey = KeyContextWrapper.getKey(view.getContext());
    SavedState savedState = getSavedState(newKey);
    view.restoreHierarchyState(savedState.getViewHierarchyState());
    if(view instanceof Bundleable) {
        ((Bundleable) view).fromBundle(savedState.getViewBundle());
    }
}
 
Example 13
Source File: MenuBuilder.java    From CSipSimple with GNU General Public License v3.0 5 votes vote down vote up
public void restoreActionViewStates(Bundle states) {
    if (states == null) {
        return;
    }

    SparseArray<Parcelable> viewStates = states.getSparseParcelableArray(
            getActionViewStatesKey());

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB && viewStates == null) {
        //Fixes Issue #652 with sdk <= 2.3.6
        return;
    }

    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) {
            v.restoreHierarchyState(viewStates);
        }
        if (item.hasSubMenu()) {
            final SubMenuBuilder subMenu = (SubMenuBuilder) item.getSubMenu();
            subMenu.restoreActionViewStates(states);
        }
    }

    final int expandedId = states.getInt(EXPANDED_ACTION_VIEW_ID);
    if (expandedId > 0) {
        MenuItem itemToExpand = findItem(expandedId);
        if (itemToExpand != null) {
            itemToExpand.expandActionView();
        }
    }
}
 
Example 14
Source File: BackstackFrame.java    From fragnums with Apache License 2.0 4 votes vote down vote up
public void restore(View view) {
  view.restoreHierarchyState(viewState);
}
 
Example 15
Source File: Presenter.java    From Mortar-architect with MIT License 4 votes vote down vote up
void present(final Dispatcher.Dispatch newDispatch, final History.Entry previousEntry, final ViewTransitionDirection direction, final Dispatcher.Callback callback) {
        Preconditions.checkNotNull(view, "Container view cannot be null");
        Preconditions.checkNull(dispatchingCallback, "Previous dispatching callback not completed");

        Logger.d("Present new dispatch: %s - with direction: %s", newDispatch.entry.scopeName, direction);
        Logger.d("Previous entry: %s", previousEntry.scopeName);

        // set and track the callback from dispatcher
        // dispatcher is waiting for the onComplete call
        // either when present is done, or when presenter is desactivated
        dispatchingCallback = callback;

        if (!previousEntry.dead) {
            // save previous view state
            Logger.d("Save view state for: %s", previousEntry.scopeName);
            previousEntry.state = getCurrentViewState();
        }

        // create or reuse view
        View newView;
        boolean addNewView;
        if (!previousEntry.dead || (previousEntry.dead && !previousEntry.isModal())) {

//                direction == Dispatcher.Direction.FORWARD ||
//                (direction == Dispatcher.Direction.BACKWARD && !previousEntry.isModal())) {
            // create new view when forward and replace
            // or when backward if previous entry is not modal
            Logger.d("Create new view for %s", newDispatch.entry.scopeName);
            Context context = newDispatch.scope.createContext(view.getContext());
            newView = newDispatch.entry.path.createView(context, view);
            addNewView = true;
        } else {
            Logger.d("Reuse previous view for %s", newDispatch.entry.scopeName);
            newView = view.getChildAt(view.getChildCount() - 2);
            addNewView = false;
        }

        // find transition
        ViewTransition transition;
        if (view.hasCurrentView()) {
            transition = transitions.findTransition(view.getCurrentView(), newView, direction);
        } else {
            transition = null;
        }

        // restore state if it exists
        if (newDispatch.entry.state != null) {
            Logger.d("Restore view state for: %s", newDispatch.entry.scopeName);
            newView.restoreHierarchyState(newDispatch.entry.state);
        }

        if (newDispatch.entry.receivedResult != null) {
            if (newView instanceof HasPresenter) {
                // put result
                ViewPresenter viewPresenter = ((HasPresenter) newView).getPresenter();
                if (viewPresenter instanceof ReceivesResult) {
                    ((ReceivesResult) viewPresenter).onReceivedResult(newDispatch.entry.receivedResult);
                }
            }
            newDispatch.entry.receivedResult = null;
        }

//        boolean keepPreviousView = direction == Dispatcher.Direction.FORWARD && newDispatch.entry.isModal();
        boolean keepPreviousView = !previousEntry.dead && newDispatch.entry.isModal();
        Logger.d("Keep previous view: %b", keepPreviousView);

        view.show(new NavigatorView.Presentation(newView, addNewView, !keepPreviousView, direction, transition), new PresentationCallback() {
            @Override
            public void onPresentationFinished(int sessionId) {
                if (isCurrentSession(sessionId)) {
                    completeDispatchingCallback();
                }
            }
        });
    }
 
Example 16
Source File: Screen.java    From scoop with Apache License 2.0 4 votes vote down vote up
public void restoreViewState(View view) {
    view.restoreHierarchyState(viewState);
}
 
Example 17
Source File: State.java    From flowless with Apache License 2.0 4 votes vote down vote up
public void restore(@NonNull View view) {
    if(viewState != null) {
        view.restoreHierarchyState(viewState);
    }
}
 
Example 18
Source File: ViewStack.java    From Defrag with Apache License 2.0 4 votes vote down vote up
void restoreState(@NonNull View view) {
	if (viewState != null) {
		view.restoreHierarchyState(viewState);
	}
}
 
Example 19
Source File: TabSwitcherDecorator.java    From ChromeLikeTabSwitcher with Apache License 2.0 4 votes vote down vote up
/**
 * The method, which is invoked by a {@link TabSwitcher} to apply the decorator. 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>onShowTab(...):void</code>.
 *
 * @param context
 *         The context, the tab switcher belongs to, as an instance of the class {@link
 *         Context}. The context may not be null
 * @param tabSwitcher
 *         The tab switcher, whose tabs are visualized by the decorator, as an instance of the
 *         class {@link TabSwitcher}. The tab switcher may not be null
 * @param view
 *         The view, which is used to visualize the tab, as an instance of the class {@link
 *         View}. The view may not be null
 * @param tab
 *         The tab, which should be visualized, as an instance of the class {@link Tab}. The tab
 *         may not be null
 * @param index
 *         The index of the tab, which should be visualized, as an {@link Integer} value
 * @param savedInstanceState
 *         The bundle, which has previously been used to save the state of the view as an
 *         instance of the class {@link Bundle} or null, if no saved state is available
 * @param inflated
 *         True, if the view has been inflated, false, if it has been reused
 */
public final void applyDecorator(@NonNull final Context context,
                                 @NonNull final TabSwitcher tabSwitcher,
                                 @NonNull final View view, @NonNull final Tab tab,
                                 final int index, @Nullable final Bundle savedInstanceState,
                                 final boolean inflated) {
    setCurrentParentView(view);
    int viewType = getViewType(tab, index);

    if (savedInstanceState != null) {
        SparseArray<Parcelable> viewStates =
                savedInstanceState.getSparseParcelableArray(VIEW_HIERARCHY_STATE_EXTRA);

        if (viewStates != null) {
            view.restoreHierarchyState(viewStates);
        }
    }

    if (!inflated) {
        onRecycleView(context, tabSwitcher, view, tab, index, viewType, savedInstanceState);
    }

    onShowTab(context, tabSwitcher, view, tab, index, viewType, savedInstanceState);
}
 
Example 20
Source File: State.java    From flow with Apache License 2.0 4 votes vote down vote up
public void restore(@NonNull View view) {
  SparseArray<Parcelable> viewState = viewStateById.get(view.getId());
  if (viewState != null) {
    view.restoreHierarchyState(viewState);
  }
}