public abstract class

AbstractSavedStateRegistry<S>

extends java.lang.Object

implements SavedStateRegistry<java.lang.Object>

 java.lang.Object

↳androidx.savedstate.AbstractSavedStateRegistry<S>

Subclasses:

BundleSavedStateRegistry

Gradle dependencies

compile group: 'androidx.savedstate', name: 'savedstate-common', version: '1.0.0-alpha01'

  • groupId: androidx.savedstate
  • artifactId: savedstate-common
  • version: 1.0.0-alpha01

Artifact androidx.savedstate:savedstate-common:1.0.0-alpha01 it located at Google repository (https://maven.google.com/)

Overview

This class provides a skeletal implementation of the SavedStateRegistry.

Implementations simply need to call AbstractSavedStateRegistry.restoreSavedState(Map) to initialize restored state and call AbstractSavedStateRegistry.saveState() once system requests saved state.

Summary

Constructors
publicAbstractSavedStateRegistry()

Methods
public final java.lang.ObjectconsumeRestoredStateForKey(java.lang.String key)

public final booleanisRestored()

Returns if state was restored after creation and can be safely consumed with AbstractSavedStateRegistry.consumeRestoredStateForKey(String)

public final voidregisterSavedStateProvider(java.lang.String key, SavedStateRegistry.SavedStateProvider<java.lang.Object> provider)

protected final voidrestoreSavedState(java.util.Map<java.lang.String, java.lang.Object> initialState)

Subclasses of this AbstractSavedStateRegistry should call this method to initialize restored state.

protected final java.util.Map<java.lang.String, java.lang.Object>saveState()

Subclasses of this AbstractSavedStateRegistry should call this method to perform state saving, this method will call all registered providers and merge a state provided by them with all unconsumed values since previous restoration.

public final voidunregisterSavedStateProvider(java.lang.String key)

Unregisters a component previously registered by the given key

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public AbstractSavedStateRegistry()

Methods

public final java.lang.Object consumeRestoredStateForKey(java.lang.String key)

public final void registerSavedStateProvider(java.lang.String key, SavedStateRegistry.SavedStateProvider<java.lang.Object> provider)

public final void unregisterSavedStateProvider(java.lang.String key)

Unregisters a component previously registered by the given key

Parameters:

key: a key with which a component was previously registered.

public final boolean isRestored()

Returns if state was restored after creation and can be safely consumed with AbstractSavedStateRegistry.consumeRestoredStateForKey(String)

Returns:

true if state was restored.

protected final void restoreSavedState(java.util.Map<java.lang.String, java.lang.Object> initialState)

Subclasses of this AbstractSavedStateRegistry should call this method to initialize restored state.

protected final java.util.Map<java.lang.String, java.lang.Object> saveState()

Subclasses of this AbstractSavedStateRegistry should call this method to perform state saving, this method will call all registered providers and merge a state provided by them with all unconsumed values since previous restoration.

Returns:

state that should be saved.

Source

/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.savedstate;

import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.arch.core.internal.SafeIterableMap;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * This class provides a skeletal implementation of the {@link SavedStateRegistry}.
 * <p>
 * Implementations simply need to call {@link #restoreSavedState(Map)} to initialize restored state
 * and call {@link #saveState()} once system requests saved state.
 *
 * @param <S> represents a class for saving a state, typically it is {@link android.os.Bundle}
 *
 * @see androidx.activity.BundleSavedStateRegistry
 */
public abstract class AbstractSavedStateRegistry<S> implements SavedStateRegistry<S> {
    private SafeIterableMap<String, SavedStateProvider<S>> mComponents =
            new SafeIterableMap<>();
    private Map<String, S> mSavedState;
    private boolean mRestored;

    @MainThread
    @Nullable
    @Override
    public final S consumeRestoredStateForKey(@NonNull String key) {
        if (!mRestored) {
            throw new IllegalStateException("You can consumeRestoredStateForKey "
                    + "only after super.onCreate of corresponding component");
        }
        S state = null;
        if (mSavedState != null) {
            state = mSavedState.remove(key);
            if (mSavedState.isEmpty()) {
                mSavedState = null;
            }
        }
        return state;
    }

    @MainThread
    @Override
    public final void registerSavedStateProvider(@NonNull String key,
            @NonNull SavedStateProvider<S> provider) {
        SavedStateProvider<S> previous = mComponents.putIfAbsent(key, provider);
        if (previous != null) {
            throw new IllegalArgumentException("SavedStateProvider with the given key is"
                    + " already registered");
        }
    }

    /**
     * Unregisters a component previously registered by the given {@code key}
     *
     * @param key a key with which a component was previously registered.
     */
    @MainThread
    @Override
    public final void unregisterSavedStateProvider(@NonNull String key) {
        mComponents.remove(key);
    }

    /**
     * Returns if state was restored after creation and can be safely consumed
     * with {@link #consumeRestoredStateForKey(String)}
     *
     * @return true if state was restored.
     */
    @MainThread
    @Override
    public final boolean isRestored() {
        return mRestored;
    }

    /**
     * Subclasses of this {@code AbstractSavedStateRegistry} should call this
     * method to initialize restored state.
     */
    @SuppressWarnings("WeakerAccess")
    @MainThread
    protected final void restoreSavedState(@Nullable Map<String, S> initialState) {
        if (initialState != null) {
            mSavedState = new HashMap<>(initialState);
        }
        mRestored = true;
    }

    /**
     * Subclasses of this {@code AbstractSavedStateRegistry} should call this
     * method to perform state saving, this method will call all registered providers and
     * merge a state provided by them with all unconsumed values since previous restoration.
     *
     * @return state that should be saved.
     */
    @MainThread
    @NonNull
    protected final Map<String, S> saveState() {
        Map<String, S> savedState = new HashMap<>();
        if (mSavedState != null) {
            savedState.putAll(mSavedState);
        }
        for (Iterator<Map.Entry<String, SavedStateProvider<S>>> it =
                mComponents.iteratorWithAdditions(); it.hasNext(); ) {
            Map.Entry<String, SavedStateProvider<S>> entry = it.next();
            savedState.put(entry.getKey(), entry.getValue().saveState());
        }
        return savedState;
    }
}