Home | History | Annotate | Download | only in activity
      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.lifecycle.activity;
     18 
     19 import android.content.Context;
     20 import android.content.Intent;
     21 import android.os.Bundle;
     22 
     23 import androidx.annotation.Nullable;
     24 import androidx.appcompat.app.AppCompatActivity;
     25 import androidx.fragment.app.Fragment;
     26 import androidx.lifecycle.Lifecycle;
     27 import androidx.lifecycle.LifecycleObserver;
     28 import androidx.lifecycle.LifecycleOwner;
     29 import androidx.lifecycle.OnLifecycleEvent;
     30 import androidx.lifecycle.extensions.test.R;
     31 
     32 import java.util.ArrayList;
     33 import java.util.Collections;
     34 import java.util.List;
     35 import java.util.concurrent.CountDownLatch;
     36 import java.util.concurrent.TimeUnit;
     37 
     38 public class FragmentLifecycleActivity extends AppCompatActivity {
     39     public static final String NESTED_TAG = "nested_fragment";
     40     public static final String MAIN_TAG = "main_fragment";
     41     private static final String EXTRA_NESTED = "nested";
     42 
     43     private final List<Lifecycle.Event> mLoggedEvents = Collections
     44             .synchronizedList(new ArrayList<Lifecycle.Event>());
     45     private LifecycleOwner mObservedOwner;
     46     private final CountDownLatch mDestroyLatch = new CountDownLatch(1);
     47 
     48     @Override
     49     protected void onCreate(Bundle savedInstanceState) {
     50         super.onCreate(savedInstanceState);
     51         setContentView(R.layout.activity_main);
     52         MainFragment fragment;
     53         fragment = new MainFragment();
     54         boolean nested = getIntent().getBooleanExtra(EXTRA_NESTED, false);
     55         if (nested) {
     56             fragment.mNestedFragment = new NestedFragment();
     57         }
     58         observe(nested ? fragment.mNestedFragment : fragment);
     59         getSupportFragmentManager().beginTransaction()
     60                 .add(R.id.fragment_container, fragment, MAIN_TAG)
     61                 .commit();
     62     }
     63 
     64     @Override
     65     protected void onDestroy() {
     66         super.onDestroy();
     67         mDestroyLatch.countDown();
     68     }
     69 
     70     public void resetEvents() {
     71         mLoggedEvents.clear();
     72     }
     73 
     74     public static class MainFragment extends Fragment {
     75         @Nullable
     76         Fragment mNestedFragment;
     77 
     78         @Override
     79         public void onCreate(@Nullable Bundle savedInstanceState) {
     80             super.onCreate(savedInstanceState);
     81             if (mNestedFragment != null) {
     82                 getChildFragmentManager().beginTransaction()
     83                         .add(mNestedFragment, NESTED_TAG)
     84                         .commit();
     85             }
     86         }
     87     }
     88 
     89     public static class NestedFragment extends Fragment {
     90     }
     91 
     92     public static Intent intentFor(Context context, boolean nested) {
     93         Intent intent = new Intent(context, FragmentLifecycleActivity.class);
     94         intent.putExtra(EXTRA_NESTED, nested);
     95         return intent;
     96     }
     97 
     98     public void observe(LifecycleOwner provider) {
     99         mObservedOwner = provider;
    100         provider.getLifecycle().addObserver(new LifecycleObserver() {
    101             @OnLifecycleEvent(Lifecycle.Event.ON_ANY)
    102             public void anyEvent(@SuppressWarnings("unused") LifecycleOwner owner,
    103                     Lifecycle.Event event) {
    104                 mLoggedEvents.add(event);
    105             }
    106         });
    107     }
    108 
    109     public List<Lifecycle.Event> getLoggedEvents() {
    110         return mLoggedEvents;
    111     }
    112 
    113     public LifecycleOwner getObservedOwner() {
    114         return mObservedOwner;
    115     }
    116 
    117     public boolean awaitForDestruction(long timeout, TimeUnit timeUnit)
    118             throws InterruptedException {
    119         return mDestroyLatch.await(timeout, timeUnit);
    120     }
    121 }
    122