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 package androidx.leanback.app; 17 18 import android.content.Intent; 19 import android.os.Bundle; 20 import android.util.Log; 21 22 import androidx.fragment.app.Fragment; 23 import androidx.fragment.app.FragmentActivity; 24 import androidx.fragment.app.FragmentTransaction; 25 import androidx.leanback.test.R; 26 27 public class SingleSupportFragmentTestActivity extends FragmentActivity { 28 29 /** 30 * Fragment that will be added to activity 31 */ 32 public static final String EXTRA_FRAGMENT_NAME = "fragmentName"; 33 34 public static final String EXTRA_ACTIVITY_LAYOUT = "activityLayout"; 35 36 public static final String EXTRA_UI_VISIBILITY = "uiVisibility"; 37 38 public static final String EXTRA_OVERRIDDEN_SAVED_INSTANCE_STATE = 39 "overriddenSavedInstanceState"; 40 41 private static final String TAG = "TestActivity"; 42 overrideSavedInstance(Bundle savedInstance)43 private Bundle overrideSavedInstance(Bundle savedInstance) { 44 Intent intent = getIntent(); 45 if (intent != null) { 46 Bundle b = intent.getBundleExtra(EXTRA_OVERRIDDEN_SAVED_INSTANCE_STATE); 47 if (b != null) { 48 return b; 49 } 50 } 51 return savedInstance; 52 } 53 54 @Override onCreate(Bundle savedInstanceState)55 public void onCreate(Bundle savedInstanceState) { 56 savedInstanceState = overrideSavedInstance(savedInstanceState); 57 super.onCreate(savedInstanceState); 58 Log.d(TAG, "onCreate " + this); 59 Intent intent = getIntent(); 60 61 final int uiOptions = intent.getIntExtra(EXTRA_UI_VISIBILITY, 0); 62 if (uiOptions != 0) { 63 getWindow().getDecorView().setSystemUiVisibility(uiOptions); 64 } 65 66 setContentView(intent.getIntExtra(EXTRA_ACTIVITY_LAYOUT, R.layout.single_fragment)); 67 if (savedInstanceState == null && findViewById(R.id.main_frame) != null) { 68 try { 69 Fragment fragment = (Fragment) Class.forName( 70 intent.getStringExtra(EXTRA_FRAGMENT_NAME)).newInstance(); 71 FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 72 ft.replace(R.id.main_frame, fragment); 73 ft.commit(); 74 } catch (Exception ex) { 75 ex.printStackTrace(); 76 finish(); 77 } 78 } 79 } 80 81 @Override onRestoreInstanceState(Bundle savedInstanceState)82 protected void onRestoreInstanceState(Bundle savedInstanceState) { 83 super.onRestoreInstanceState(overrideSavedInstance(savedInstanceState)); 84 } 85 performSaveInstanceState()86 public Bundle performSaveInstanceState() { 87 Bundle state = new Bundle(); 88 onSaveInstanceState(state); 89 return state; 90 } 91 getTestFragment()92 public Fragment getTestFragment() { 93 return getSupportFragmentManager().findFragmentById(R.id.main_frame); 94 } 95 } 96