Created
September 22, 2015 18:32
-
-
Save fredgrott/adc8398c626ca924f204 to your computer and use it in GitHub Desktop.
BaseSavingState
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.grottworkshop.gwsbase; | |
import android.os.Bundle; | |
import java.util.List; | |
@SuppressWarnings("unused") | |
public abstract class BaseSavingState implements BaseSaveable { | |
public List<? extends BaseSaveable> componentsToSave; | |
private BaseSavingState mInstance; | |
/** | |
* Format, ie how to ove-ride | |
* | |
* <code> | |
* | |
* public init(ObjectOne objectOne, ObjectTwo){ | |
* componentsToSave = (Arrays.asList( | |
* objectyOne, | |
* objectTwo)); | |
* } | |
* | |
* | |
* | |
* </code> | |
* | |
* Make sure to use your extended BaseSavingStateProvider.getInstance to | |
* grab a singleton in initGlobalSingletons() of your extended appclass | |
* see javadoc of BaseSavingStateProvider | |
* | |
*/ | |
public BaseSavingState(){ | |
} | |
@Override | |
public void saveInstanceState(Bundle bundle) { | |
Bundle instanceState = new Bundle(); | |
for (BaseSaveable saveable : componentsToSave) { | |
saveable.saveInstanceState(instanceState); | |
} | |
bundle.putBundle("global-state", instanceState); | |
} | |
@Override | |
public void restoreInstanceState(Bundle bundle) { | |
if (bundle == null) { | |
return; | |
} | |
Bundle instanceState = bundle.getBundle("global-state"); | |
if (instanceState == null) { | |
return; | |
} | |
for (BaseSaveable saveable : componentsToSave) { | |
saveable.restoreInstanceState(instanceState); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment