react-native-navigation的迁移库

JsDevReloadHandler.java 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package com.reactnativenavigation.react;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.IntentFilter;
  6. import android.view.KeyEvent;
  7. import android.view.View;
  8. import android.widget.EditText;
  9. import com.facebook.react.ReactInstanceManager;
  10. import com.reactnativenavigation.NavigationApplication;
  11. class JsDevReloadHandler {
  12. private static boolean shouldRefreshOnRR = false;
  13. private final BroadcastReceiver reloadReceiver = new BroadcastReceiver() {
  14. @Override
  15. public void onReceive(Context context, Intent intent) {
  16. reload();
  17. }
  18. };
  19. void onResumeActivity() {
  20. if (getReactInstanceManager().getDevSupportManager().getDevSupportEnabled()) {
  21. NavigationApplication.instance.registerReceiver(reloadReceiver, new IntentFilter("react.native.RELOAD"));
  22. }
  23. }
  24. void onPauseActivity() {
  25. if (getReactInstanceManager().getDevSupportManager().getDevSupportEnabled()) {
  26. NavigationApplication.instance.unregisterReceiver(reloadReceiver);
  27. }
  28. }
  29. boolean onKeyUp(View currentFocus, int keyCode) {
  30. ReactInstanceManager reactInstanceManager = getReactInstanceManager();
  31. if (reactInstanceManager != null &&
  32. reactInstanceManager.getDevSupportManager().getDevSupportEnabled()) {
  33. if (keyCode == KeyEvent.KEYCODE_MENU) {
  34. reactInstanceManager.showDevOptionsDialog();
  35. return true;
  36. }
  37. if (keyCode == KeyEvent.KEYCODE_R && !(currentFocus instanceof EditText)) {
  38. // Enable double-tap-R-to-reload
  39. if (shouldRefreshOnRR) {
  40. reload();
  41. return true;
  42. } else {
  43. shouldRefreshOnRR = true;
  44. NavigationApplication.instance.runOnMainThread(
  45. new Runnable() {
  46. @Override
  47. public void run() {
  48. shouldRefreshOnRR = false;
  49. }
  50. },
  51. 500);
  52. }
  53. }
  54. }
  55. return false;
  56. }
  57. private void reload() {
  58. getReactInstanceManager().getDevSupportManager().handleReloadJS();
  59. shouldRefreshOnRR = false;
  60. }
  61. private ReactInstanceManager getReactInstanceManager() {
  62. return NavigationApplication
  63. .instance
  64. .getReactGateway()
  65. .getReactInstanceManager();
  66. }
  67. }