|
| 1 | +// Copyright 2021 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.googletest.firebase.perf.testapp; |
| 16 | + |
| 17 | +import android.os.Bundle; |
| 18 | +import android.util.Log; |
| 19 | +import android.view.Menu; |
| 20 | +import android.view.MenuItem; |
| 21 | +import android.view.View; |
| 22 | +import androidx.annotation.NonNull; |
| 23 | +import androidx.annotation.Nullable; |
| 24 | +import androidx.appcompat.app.AppCompatActivity; |
| 25 | +import androidx.appcompat.widget.Toolbar; |
| 26 | +import androidx.fragment.app.Fragment; |
| 27 | +import androidx.fragment.app.FragmentManager; |
| 28 | +import androidx.lifecycle.ViewModelProvider; |
| 29 | +import androidx.navigation.NavController; |
| 30 | +import androidx.navigation.Navigation; |
| 31 | +import androidx.navigation.ui.AppBarConfiguration; |
| 32 | +import androidx.navigation.ui.NavigationUI; |
| 33 | +import com.google.android.material.bottomnavigation.BottomNavigationView; |
| 34 | + |
| 35 | +public class FragmentActivity extends AppCompatActivity { |
| 36 | + SharedViewModel model; |
| 37 | + |
| 38 | + @Override |
| 39 | + protected void onCreate(Bundle savedInstanceState) { |
| 40 | + // Listening on FragmentManager's FragmentLifeCycleCallbacks |
| 41 | + registerListeners(); |
| 42 | + super.onCreate(savedInstanceState); |
| 43 | + setContentView(R.layout.activity_fragment); |
| 44 | + Toolbar toolbar = findViewById(R.id.toolbar_fragment_activity); |
| 45 | + setSupportActionBar(toolbar); |
| 46 | + BottomNavigationView navView = findViewById(R.id.nav_view); |
| 47 | + // Passing each menu ID as a set of Ids because each |
| 48 | + // menu should be considered as top level destinations. |
| 49 | + AppBarConfiguration appBarConfiguration = |
| 50 | + new AppBarConfiguration.Builder( |
| 51 | + R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications) |
| 52 | + .build(); |
| 53 | + NavController navController = |
| 54 | + Navigation.findNavController(this, R.id.nav_host_fragment_activity_fragment); |
| 55 | + // Listening on navigation events using NavController |
| 56 | + navController.addOnDestinationChangedListener( |
| 57 | + (controller, destination, arguments) -> { |
| 58 | + Log.d("Navigation", destination.getLabel().toString() + "Fragment"); |
| 59 | + }); |
| 60 | + NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration); |
| 61 | + NavigationUI.setupWithNavController( |
| 62 | + (BottomNavigationView) findViewById(R.id.nav_view), navController); |
| 63 | + model = new ViewModelProvider(this).get(SharedViewModel.class); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 68 | + getMenuInflater().inflate(R.menu.menu, menu); |
| 69 | + return true; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 74 | + switch (item.getItemId()) { |
| 75 | + case R.id.action_change_gif: |
| 76 | + model.changeImage(); |
| 77 | + return true; |
| 78 | + default: |
| 79 | + return super.onOptionsItemSelected(item); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + protected void onStart() { |
| 85 | + super.onStart(); |
| 86 | + Log.d( |
| 87 | + "FragmentActivity", |
| 88 | + "onStart; View Hardware Accel: " |
| 89 | + + decorViewAcceleratedFlag() |
| 90 | + + ", FrameMetricsObservers won't be observing, only queued."); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public void onAttachedToWindow() { |
| 95 | + super.onAttachedToWindow(); |
| 96 | + Log.d( |
| 97 | + "FragmentActivity", |
| 98 | + "onAttachedToWindow; View Hardware Accel: " |
| 99 | + + decorViewAcceleratedFlag() |
| 100 | + + ", FrameMetricsObservers in queue and any new observers will start observing."); |
| 101 | + } |
| 102 | + |
| 103 | + private boolean decorViewAcceleratedFlag() { |
| 104 | + View v = this.getWindow().getDecorView(); |
| 105 | + return v.isHardwareAccelerated(); |
| 106 | + } |
| 107 | + |
| 108 | + private void registerListeners() { |
| 109 | + getSupportFragmentManager() |
| 110 | + .registerFragmentLifecycleCallbacks( |
| 111 | + new FragmentManager.FragmentLifecycleCallbacks() { |
| 112 | + @Override |
| 113 | + public void onFragmentCreated( |
| 114 | + @NonNull FragmentManager fm, |
| 115 | + @NonNull Fragment f, |
| 116 | + @Nullable Bundle savedInstanceState) { |
| 117 | + super.onFragmentCreated(fm, f, savedInstanceState); |
| 118 | + Log.d("FragmentManager", "Fragment created " + f.getClass().getSimpleName()); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public void onFragmentViewCreated( |
| 123 | + @NonNull FragmentManager fm, |
| 124 | + @NonNull Fragment f, |
| 125 | + @NonNull View v, |
| 126 | + @Nullable Bundle savedInstanceState) { |
| 127 | + super.onFragmentViewCreated(fm, f, v, savedInstanceState); |
| 128 | + Log.d("FragmentManager", "View created " + f.getClass().getSimpleName()); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public void onFragmentStarted(@NonNull FragmentManager fm, @NonNull Fragment f) { |
| 133 | + super.onFragmentStarted(fm, f); |
| 134 | + Log.d("FragmentManager", "Fragment started " + f.getClass().getSimpleName()); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public void onFragmentResumed(@NonNull FragmentManager fm, @NonNull Fragment f) { |
| 139 | + super.onFragmentResumed(fm, f); |
| 140 | + Log.d("FragmentManager", "Fragment resumed " + f.getClass().getSimpleName()); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public void onFragmentStopped(@NonNull FragmentManager fm, @NonNull Fragment f) { |
| 145 | + super.onFragmentStopped(fm, f); |
| 146 | + Log.d("FragmentManager", "Fragment stopped " + f.getClass().getSimpleName()); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public void onFragmentViewDestroyed( |
| 151 | + @NonNull FragmentManager fm, @NonNull Fragment f) { |
| 152 | + super.onFragmentViewDestroyed(fm, f); |
| 153 | + Log.d("FragmentManager", "View destroyed " + f.getClass().getSimpleName()); |
| 154 | + } |
| 155 | + }, |
| 156 | + true); |
| 157 | + } |
| 158 | +} |
0 commit comments