1 /* 2 * Copyright (C) 2021 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 com.android.car.settings.common; 18 19 import android.content.Context; 20 import android.content.Intent; 21 import android.os.Bundle; 22 23 import androidx.annotation.NonNull; 24 import androidx.annotation.Nullable; 25 import androidx.fragment.app.Fragment; 26 27 /** 28 * Activity class for launching a SubSetting as a separate activity. 29 */ 30 public class SubSettingsActivity extends BaseCarSettingsActivity { 31 32 private static final String KEY_SUB_SETTINGS_FRAGMENT = "key_sub_settings_fragment"; 33 private static final String KEY_SUB_SETTINGS_FRAGMENT_ARGS = "key_sub_settings_fragment_args"; 34 private static final Logger LOG = new Logger(SubSettingsActivity.class); 35 36 /** 37 * Create a new SubSettingsActivity instance for the given fragment. 38 */ newInstance(@onNull Context context, @NonNull Fragment fragment)39 public static Intent newInstance(@NonNull Context context, @NonNull Fragment fragment) { 40 String fragmentClass = fragment.getClass().getName(); 41 Intent intent = new Intent(context, SubSettingsActivity.class); 42 intent.putExtra(KEY_SUB_SETTINGS_FRAGMENT, fragmentClass); 43 intent.putExtra(KEY_SUB_SETTINGS_FRAGMENT_ARGS, fragment.getArguments()); 44 return intent; 45 } 46 47 @Nullable 48 @Override getInitialFragment()49 protected Fragment getInitialFragment() { 50 try { 51 String fragmentClass = getIntent().getStringExtra(KEY_SUB_SETTINGS_FRAGMENT); 52 Bundle fragmentArgs = getIntent().getBundleExtra(KEY_SUB_SETTINGS_FRAGMENT_ARGS); 53 Fragment fragment = getSupportFragmentManager().getFragmentFactory().instantiate( 54 getClassLoader(), fragmentClass); 55 fragment.setArguments(fragmentArgs); 56 return fragment; 57 } catch (Fragment.InstantiationException e) { 58 LOG.e("Unable to instantiate fragment", e); 59 return null; 60 } 61 } 62 } 63