/** * Ensures a pattern is initialized and active. If the pattern doesn't already exist, one is * created and (if the activity/fragment is currently started). Otherwise the last created pattern * is re-used. * * <p>In either case, the given callback is associated with the pattern, and will be called as the * pattern state changes. * * @param id A unique identifier for this pattern. Can be whatever you want. Identifiers are * scoped to a particular PatternManager instance. * @param args Optional arguments to supply to the pattern at construction. If a pattern already * exists (a new one does not need to be created), this parameter will be ignored and the last * arguments continue to be used. * @param callback Interface the PatternManager will call to report about changes in the state of * the pattern. Required. * @return */ public Pattern initPattern(int id, Bundle args, PatternCallbacks callback) { RetainedPatternArray patternArray = getRetainedPatternArray(); if (patternArray == null) { patternArray = new RetainedPatternArray(); getRetainedFragment().getRetainedSystemInstanceMap().put(KEY, patternArray); } Pattern pattern = patternArray.get(id); if (pattern == null) { pattern = callback.onCreatePattern(id, args); patternArray.put(id, pattern); } return pattern; }
/** * Return the Pattern with the given id or {@code null} if no matching Pattern is found. * * @param id A unique identifier for this pattern. Can be whatever you want. Identifiers are * scoped to a particular PatternManager instance. * @return The pattern with the given id or {@code null} if no matching Pattern is found. */ public Pattern getPattern(int id) { RetainedPatternArray patternArray = getRetainedPatternArray(); return (patternArray == null ? null : patternArray.get(id)); }