1 // Copyright 2013 The Flutter Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package io.flutter.app; 6 7 import android.content.Context; 8 import android.content.Intent; 9 import android.content.res.Configuration; 10 import android.os.Bundle; 11 import android.support.v4.app.FragmentActivity; 12 import io.flutter.app.FlutterActivityDelegate.ViewFactory; 13 import io.flutter.plugin.common.PluginRegistry; 14 import io.flutter.view.FlutterNativeView; 15 import io.flutter.view.FlutterView; 16 17 /** 18 * Base class for activities that use Flutter who also require the use of the 19 * Android v4 Support library's {@link FragmentActivity}. Applications that 20 * don't have this need will likely want to use {@link FlutterActivity} instead. 21 * 22 * <p><strong>Important!</strong> Flutter does not bundle the necessary Android 23 * v4 Support library classes for this class to work at runtime. It is the 24 * responsibility of the app developer using this class to ensure that they 25 * link against the v4 support library .jar file when creating their app to 26 * ensure that {@link FragmentActivity} is available at runtime.</p> 27 * 28 * @see <a target="_new" href="https://developer.android.com/topic/libraries/support-library/setup.html">https://developer.android.com/topic/libraries/support-library/setup.html</a> 29 */ 30 public class FlutterFragmentActivity 31 extends FragmentActivity implements FlutterView.Provider, PluginRegistry, ViewFactory { 32 private final FlutterActivityDelegate delegate = new FlutterActivityDelegate(this, this); 33 34 // These aliases ensure that the methods we forward to the delegate adhere 35 // to relevant interfaces versus just existing in FlutterActivityDelegate. 36 private final FlutterActivityEvents eventDelegate = delegate; 37 private final FlutterView.Provider viewProvider = delegate; 38 private final PluginRegistry pluginRegistry = delegate; 39 40 /** 41 * Returns the Flutter view used by this activity; will be null before 42 * {@link #onCreate(Bundle)} is called. 43 */ 44 @Override getFlutterView()45 public FlutterView getFlutterView() { 46 return viewProvider.getFlutterView(); 47 } 48 49 /** 50 * Hook for subclasses to customize the creation of the 51 * {@code FlutterView}. 52 * 53 * <p>The default implementation returns {@code null}, which will cause the 54 * activity to use a newly instantiated full-screen view.</p> 55 */ 56 @Override createFlutterView(Context context)57 public FlutterView createFlutterView(Context context) { 58 return null; 59 } 60 61 @Override createFlutterNativeView()62 public FlutterNativeView createFlutterNativeView() { 63 return null; 64 } 65 66 @Override retainFlutterNativeView()67 public boolean retainFlutterNativeView() { 68 return false; 69 } 70 71 @Override hasPlugin(String key)72 public final boolean hasPlugin(String key) { 73 return pluginRegistry.hasPlugin(key); 74 } 75 76 @Override valuePublishedByPlugin(String pluginKey)77 public final <T> T valuePublishedByPlugin(String pluginKey) { 78 return pluginRegistry.valuePublishedByPlugin(pluginKey); 79 } 80 81 @Override registrarFor(String pluginKey)82 public final Registrar registrarFor(String pluginKey) { 83 return pluginRegistry.registrarFor(pluginKey); 84 } 85 86 @Override onCreate(Bundle savedInstanceState)87 protected void onCreate(Bundle savedInstanceState) { 88 super.onCreate(savedInstanceState); 89 eventDelegate.onCreate(savedInstanceState); 90 } 91 92 @Override onDestroy()93 protected void onDestroy() { 94 eventDelegate.onDestroy(); 95 super.onDestroy(); 96 } 97 98 @Override onBackPressed()99 public void onBackPressed() { 100 if (!eventDelegate.onBackPressed()) { 101 super.onBackPressed(); 102 } 103 } 104 105 @Override onStart()106 protected void onStart() { 107 super.onStart(); 108 eventDelegate.onStart(); 109 } 110 111 @Override onStop()112 protected void onStop() { 113 eventDelegate.onStop(); 114 super.onStop(); 115 } 116 117 @Override onPause()118 protected void onPause() { 119 super.onPause(); 120 eventDelegate.onPause(); 121 } 122 123 @Override onPostResume()124 protected void onPostResume() { 125 super.onPostResume(); 126 eventDelegate.onPostResume(); 127 } 128 129 // @Override - added in API level 23 onRequestPermissionsResult( int requestCode, String[] permissions, int[] grantResults)130 public void onRequestPermissionsResult( 131 int requestCode, String[] permissions, int[] grantResults) { 132 eventDelegate.onRequestPermissionsResult(requestCode, permissions, grantResults); 133 } 134 135 @Override onActivityResult(int requestCode, int resultCode, Intent data)136 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 137 if (!eventDelegate.onActivityResult(requestCode, resultCode, data)) { 138 super.onActivityResult(requestCode, resultCode, data); 139 } 140 } 141 142 @Override onNewIntent(Intent intent)143 protected void onNewIntent(Intent intent) { 144 eventDelegate.onNewIntent(intent); 145 } 146 147 @Override onUserLeaveHint()148 public void onUserLeaveHint() { 149 eventDelegate.onUserLeaveHint(); 150 } 151 152 @Override onTrimMemory(int level)153 public void onTrimMemory(int level) { 154 eventDelegate.onTrimMemory(level); 155 } 156 157 @Override onLowMemory()158 public void onLowMemory() { 159 eventDelegate.onLowMemory(); 160 } 161 162 @Override onConfigurationChanged(Configuration newConfig)163 public void onConfigurationChanged(Configuration newConfig) { 164 super.onConfigurationChanged(newConfig); 165 eventDelegate.onConfigurationChanged(newConfig); 166 } 167 } 168