1 /* 2 * Copyright (C) 2017 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 package androidx.lifecycle.testapp; 17 18 import static androidx.lifecycle.testapp.TestEvent.OWNER_CALLBACK; 19 20 import android.annotation.SuppressLint; 21 import android.os.Bundle; 22 import android.view.LayoutInflater; 23 import android.view.View; 24 import android.view.ViewGroup; 25 import android.widget.FrameLayout; 26 27 import androidx.annotation.Nullable; 28 import androidx.core.util.Pair; 29 import androidx.fragment.app.Fragment; 30 import androidx.lifecycle.Lifecycle; 31 32 import java.util.ArrayList; 33 import java.util.List; 34 35 /** 36 * A support fragment that collects all of its events. 37 */ 38 @SuppressLint("ValidFragment") 39 public class CollectingSupportFragment extends Fragment implements CollectingLifecycleOwner { 40 private final List<Pair<TestEvent, Lifecycle.Event>> mCollectedEvents = 41 new ArrayList<>(); 42 private TestObserver mTestObserver = new TestObserver(mCollectedEvents); 43 44 @Override 45 public void onCreate(Bundle savedInstanceState) { 46 super.onCreate(savedInstanceState); 47 mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Lifecycle.Event.ON_CREATE)); 48 getLifecycle().addObserver(mTestObserver); 49 } 50 51 @Nullable 52 @Override 53 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, 54 @Nullable Bundle savedInstanceState) { 55 //noinspection ConstantConditions 56 FrameLayout layout = new FrameLayout(container.getContext()); 57 layout.setId(R.id.child_fragment_container); 58 return layout; 59 } 60 61 /** 62 * Runs a replace fragment transaction with 'fragment' on this Fragment. 63 */ 64 public void replaceFragment(Fragment fragment) { 65 getChildFragmentManager() 66 .beginTransaction() 67 .add(R.id.child_fragment_container, fragment) 68 .commitNow(); 69 } 70 71 @Override 72 public void onStart() { 73 super.onStart(); 74 mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Lifecycle.Event.ON_START)); 75 } 76 77 @Override 78 public void onResume() { 79 super.onResume(); 80 mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Lifecycle.Event.ON_RESUME)); 81 } 82 83 @Override 84 public void onDestroy() { 85 super.onDestroy(); 86 mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Lifecycle.Event.ON_DESTROY)); 87 } 88 89 @Override 90 public void onStop() { 91 super.onStop(); 92 mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Lifecycle.Event.ON_STOP)); 93 } 94 95 @Override 96 public void onPause() { 97 super.onPause(); 98 mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Lifecycle.Event.ON_PAUSE)); 99 } 100 101 @Override 102 public List<Pair<TestEvent, Lifecycle.Event>> copyCollectedEvents() { 103 return new ArrayList<>(mCollectedEvents); 104 } 105 } 106