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 17 18 package android.fragment.cts; 19 20 import android.os.Bundle; 21 22 public class ReentrantFragment extends StrictFragment { 23 private static final String FROM_STATE = "fromState"; 24 private static final String TO_STATE = "toState"; 25 int mFromState = 0; 26 int mToState = 0; 27 boolean mIsRestored; 28 create(int fromState, int toState)29 public static ReentrantFragment create(int fromState, int toState) { 30 ReentrantFragment fragment = new ReentrantFragment(); 31 fragment.mFromState = fromState; 32 fragment.mToState = toState; 33 fragment.mIsRestored = false; 34 return fragment; 35 } 36 37 @Override onStateChanged(int fromState)38 public void onStateChanged(int fromState) { 39 super.onStateChanged(fromState); 40 // We execute the transaction when shutting down or after restoring 41 if (fromState == mFromState && mState == mToState 42 && (mToState < mFromState || mIsRestored)) { 43 executeTransaction(); 44 } 45 } 46 executeTransaction()47 private void executeTransaction() { 48 getFragmentManager().beginTransaction() 49 .add(new StrictFragment(), "should throw") 50 .commitNow(); 51 } 52 53 @Override onSaveInstanceState(Bundle outState)54 public void onSaveInstanceState(Bundle outState) { 55 super.onSaveInstanceState(outState); 56 outState.putInt(FROM_STATE, mFromState); 57 outState.putInt(TO_STATE, mToState); 58 } 59 60 @Override onCreate(Bundle savedInstanceState)61 public void onCreate(Bundle savedInstanceState) { 62 if (savedInstanceState != null) { 63 mFromState = savedInstanceState.getInt(FROM_STATE); 64 mToState = savedInstanceState.getInt(TO_STATE); 65 mIsRestored = true; 66 } 67 super.onCreate(savedInstanceState); 68 } 69 70 } 71