下面列出了android.view.View#restoreHierarchyState ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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 );
}
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);
}
@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;
}
@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;
}
/**
* 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();
}
};
}
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();
}
}
}
/**
* 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());
}
}
public void restore(View view) {
int originalId = view.getId();
setIdIfNoneExists(view);
view.restoreHierarchyState(this);
view.setId(originalId);
}
/**
* 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);
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
/**
* 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);
}
void restoreState(@NonNull View view) {
if (viewState != null) {
view.restoreHierarchyState(viewState);
}
}
public void restore(@NonNull View view) {
if(viewState != null) {
view.restoreHierarchyState(viewState);
}
}
public void restore(View view) {
view.restoreHierarchyState(viewState);
}
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();
}
}
});
}
public void restoreViewState(View view) {
view.restoreHierarchyState(viewState);
}
public void restore(@NonNull View view) {
SparseArray<Parcelable> viewState = viewStateById.get(view.getId());
if (viewState != null) {
view.restoreHierarchyState(viewState);
}
}