1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.core.app; 18 19 import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP; 20 21 import android.app.Activity; 22 import android.os.Bundle; 23 24 import androidx.annotation.CallSuper; 25 import androidx.annotation.Nullable; 26 import androidx.annotation.RestrictTo; 27 import androidx.collection.SimpleArrayMap; 28 import androidx.lifecycle.Lifecycle; 29 import androidx.lifecycle.LifecycleOwner; 30 import androidx.lifecycle.LifecycleRegistry; 31 import androidx.lifecycle.ReportFragment; 32 33 /** 34 * Base class for activities that enables composition of higher level components. 35 * <p> 36 * Rather than all functionality being built directly into this class, only the minimal set of 37 * lower level building blocks are included. Higher level components can then be used as needed 38 * without enforcing a deep Activity class hierarchy or strong coupling between components. 39 * 40 * @hide 41 */ 42 @RestrictTo(LIBRARY_GROUP) 43 public class ComponentActivity extends Activity implements LifecycleOwner { 44 /** 45 * Storage for {@link ExtraData} instances. 46 * 47 * <p>Note that these objects are not retained across configuration changes</p> 48 */ 49 private SimpleArrayMap<Class<? extends ExtraData>, ExtraData> mExtraDataMap = 50 new SimpleArrayMap<>(); 51 52 private LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this); 53 54 /** 55 * Store an instance of {@link ExtraData} for later retrieval by class name 56 * via {@link #getExtraData}. 57 * 58 * <p>Note that these objects are not retained across configuration changes</p> 59 * 60 * @see #getExtraData 61 * @hide 62 */ 63 @RestrictTo(LIBRARY_GROUP) putExtraData(ExtraData extraData)64 public void putExtraData(ExtraData extraData) { 65 mExtraDataMap.put(extraData.getClass(), extraData); 66 } 67 68 @Override 69 @SuppressWarnings("RestrictedApi") onCreate(@ullable Bundle savedInstanceState)70 protected void onCreate(@Nullable Bundle savedInstanceState) { 71 super.onCreate(savedInstanceState); 72 ReportFragment.injectIfNeededIn(this); 73 } 74 75 @CallSuper 76 @Override onSaveInstanceState(Bundle outState)77 protected void onSaveInstanceState(Bundle outState) { 78 mLifecycleRegistry.markState(Lifecycle.State.CREATED); 79 super.onSaveInstanceState(outState); 80 } 81 82 /** 83 * Retrieves a previously set {@link ExtraData} by class name. 84 * 85 * @see #putExtraData 86 * @hide 87 */ 88 @RestrictTo(LIBRARY_GROUP) getExtraData(Class<T> extraDataClass)89 public <T extends ExtraData> T getExtraData(Class<T> extraDataClass) { 90 return (T) mExtraDataMap.get(extraDataClass); 91 } 92 93 @Override getLifecycle()94 public Lifecycle getLifecycle() { 95 return mLifecycleRegistry; 96 } 97 98 /** 99 * @hide 100 */ 101 @RestrictTo(LIBRARY_GROUP) 102 public static class ExtraData { 103 } 104 } 105