Created
July 18, 2024 09:59
-
-
Save mdjastrzebski/b7a8f4ca080ed4e8bac603957f67c091 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.flutter_brownfield_app | |
import android.content.Intent | |
import android.view.KeyEvent | |
import com.facebook.react.ReactInstanceManager | |
import com.facebook.react.ReactNativeHost | |
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler | |
import io.flutter.embedding.android.FlutterActivity | |
// ... | |
class MainActivity: FlutterActivity(), DefaultHardwareBackBtnHandler { | |
// ... | |
// Easy access to ReactNativeHost | |
private val reactNativeHost: ReactNativeHost | |
get() = (application as MainApplication).reactNativeHost | |
// Easy access to ReactNativeHost | |
private val reactInstanceManager: ReactInstanceManager | |
get() = (application as MainApplication).reactNativeHost.reactInstanceManager | |
// ... | |
// Inform React Native Host about "onPause" lifecycle event | |
override fun onPause() { | |
super.onPause() | |
reactInstanceManager.onHostPause(this) | |
} | |
// Inform React Native Host about "onResume" lifecycle event | |
override fun onResume() { | |
super.onResume() | |
reactInstanceManager.onHostResume(this, this) | |
} | |
// Inform React Native Host about "onDestroy" lifecycle event | |
override fun onDestroy() { | |
super.onDestroy() | |
reactInstanceManager.onHostDestroy(this) | |
} | |
// Inform React Native Host about "onBackPressed" lifecycle event | |
override fun onBackPressed() { | |
reactInstanceManager.onBackPressed() | |
super.onBackPressed() | |
} | |
// Trigger Dev Options dialog when user presses "menu" button on Simulator. | |
override fun onKeyUp(keyCode: Int, event: KeyEvent): Boolean { | |
if (keyCode == KeyEvent.KEYCODE_MENU && reactNativeHost.hasInstance()) { | |
reactInstanceManager.showDevOptionsDialog() | |
return true | |
} | |
return super.onKeyUp(keyCode, event) | |
} | |
// Required by DefaultHardwareBackBtnHandler | |
override fun invokeDefaultOnBackPressed() { | |
super.onBackPressed() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment