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 17package androidx.core.app; 18 19import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP; 20 21import android.app.Activity; 22import android.os.Bundle; 23 24import androidx.annotation.CallSuper; 25import androidx.annotation.Nullable; 26import androidx.annotation.RestrictTo; 27import androidx.collection.SimpleArrayMap; 28import androidx.lifecycle.Lifecycle; 29import androidx.lifecycle.LifecycleOwner; 30import androidx.lifecycle.LifecycleRegistry; 31import 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) 43public 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) 64 public void putExtraData(ExtraData extraData) { 65 mExtraDataMap.put(extraData.getClass(), extraData); 66 } 67 68 @Override 69 @SuppressWarnings("RestrictedApi") 70 protected void onCreate(@Nullable Bundle savedInstanceState) { 71 super.onCreate(savedInstanceState); 72 ReportFragment.injectIfNeededIn(this); 73 } 74 75 @CallSuper 76 @Override 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) 89 public <T extends ExtraData> T getExtraData(Class<T> extraDataClass) { 90 return (T) mExtraDataMap.get(extraDataClass); 91 } 92 93 @Override 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