diff --git a/android/lib/src/main/java/com/ern/api/impl/core/ElectrodeBaseActivityDelegate.java b/android/lib/src/main/java/com/ern/api/impl/core/ElectrodeBaseActivityDelegate.java index b74a293b..42c03b08 100644 --- a/android/lib/src/main/java/com/ern/api/impl/core/ElectrodeBaseActivityDelegate.java +++ b/android/lib/src/main/java/com/ern/api/impl/core/ElectrodeBaseActivityDelegate.java @@ -51,7 +51,7 @@ public ElectrodeBaseActivityDelegate(@NonNull FragmentActivity activity, @Nullab @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - if (savedInstanceState == null && mDefaultLaunchConfig.startRootInOnCreate) { + if (savedInstanceState == null && mDefaultLaunchConfig.mStartRootInOnCreate) { launchRootComponent(); } } diff --git a/android/lib/src/main/java/com/ern/api/impl/core/LaunchConfig.java b/android/lib/src/main/java/com/ern/api/impl/core/LaunchConfig.java index 673f8a00..5ced0374 100644 --- a/android/lib/src/main/java/com/ern/api/impl/core/LaunchConfig.java +++ b/android/lib/src/main/java/com/ern/api/impl/core/LaunchConfig.java @@ -73,12 +73,12 @@ public class LaunchConfig { /** * Decides of the root component needs to be launched when the activity delegates onCreate is called. */ - boolean startRootInOnCreate = true; + boolean mStartRootInOnCreate = true; - /*** + /** * Set this to to true if the root page up click is handled by React native. */ - boolean rootBackPressHandledByRN = false; + boolean mRootBackPressHandledByRN; public LaunchConfig() { } @@ -178,7 +178,7 @@ public boolean isShowAsOverlay() { * @param startRootInOnCreate true | false Default value: true */ public void setStartRootInOnCreate(boolean startRootInOnCreate) { - this.startRootInOnCreate = startRootInOnCreate; + mStartRootInOnCreate = startRootInOnCreate; } /** @@ -187,7 +187,7 @@ public void setStartRootInOnCreate(boolean startRootInOnCreate) { */ public boolean isStartRootInOnCreate() { - return startRootInOnCreate; + return mStartRootInOnCreate; } /** @@ -196,7 +196,7 @@ public boolean isStartRootInOnCreate() { * @return true | false */ public void setRootBackPressHandledByRN(boolean rootBackPressHandledByRN) { - this.rootBackPressHandledByRN = rootBackPressHandledByRN; + mRootBackPressHandledByRN = rootBackPressHandledByRN; } /** @@ -205,6 +205,6 @@ public void setRootBackPressHandledByRN(boolean rootBackPressHandledByRN) { * @return true | false */ public boolean isRootBackPressHandledByRN() { - return rootBackPressHandledByRN; + return mRootBackPressHandledByRN; } } diff --git a/android/lib/src/main/java/com/ern/api/impl/navigation/ElectrodeNavigationFragmentDelegate.java b/android/lib/src/main/java/com/ern/api/impl/navigation/ElectrodeNavigationFragmentDelegate.java index f4ac8f3d..faeacf7b 100644 --- a/android/lib/src/main/java/com/ern/api/impl/navigation/ElectrodeNavigationFragmentDelegate.java +++ b/android/lib/src/main/java/com/ern/api/impl/navigation/ElectrodeNavigationFragmentDelegate.java @@ -51,13 +51,7 @@ public class ElectrodeNavigationFragmentDelegate routeObserver = new Observer() { @Override @@ -192,7 +186,8 @@ public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) { Fragment fragment = getTopOfTheStackFragment(); if (mFragment == fragment) { - if ((mOnHomeAsUpClickedCallback != null && mOnHomeAsUpClickedCallback.onHomAsUpClicked())) { + if (mBackPressedCallback.isEnabled()) { + mBackPressedCallback.handleOnBackPressed(); return true; } else { mFragment.requireActivity().onBackPressed(); @@ -376,14 +371,8 @@ private void updateNavBar(@NonNull NavigationBar navigationBar) { private void updateHomeAsUpIndicator(@Nullable NavigationBarLeftButton leftButton) { ActionBar supportActionBar = getSupportActionBar(); if (supportActionBar != null) { + mBackPressedCallback.setEnabled(leftButton); if (leftButton != null) { - if (leftButton.getId() != null) { - Logger.v(TAG, "Enabling backPressedCallback for component:%s. LeftButton has id. Back press event will now be sent to react native for handling", getReactComponentName()); - mOnHomeAsUpClickedCallback = new HomeAsUpClickedCallback(leftButton); - mBackPressedCallback.setEnabled(true); - } else { - disableBackPressCallback(); - } if (leftButton.getDisabled() != null && leftButton.getDisabled()) { Logger.d(TAG, "Disabling DisplayHomeAsUp for component: %s", getReactComponentName()); supportActionBar.setDisplayHomeAsUpEnabled(false); @@ -391,9 +380,8 @@ private void updateHomeAsUpIndicator(@Nullable NavigationBarLeftButton leftButto } else if (setHomeAsUpIndicatorIcon(supportActionBar, leftButton)) { return; } - } else { - disableBackPressCallback(); } + //Default action if (mFragment.getArguments() != null && mFragment.getArguments().getBoolean(ActivityDelegateConstants.KEY_MINI_APP_FRAGMENT_SHOW_UP_ENABLED)) { Logger.d(TAG, "Defaulting up indicator for component: %s", getReactComponentName()); @@ -405,11 +393,6 @@ private void updateHomeAsUpIndicator(@Nullable NavigationBarLeftButton leftButto } } - private void disableBackPressCallback() { - mOnHomeAsUpClickedCallback = null; - mBackPressedCallback.setEnabled(false); - } - private boolean setHomeAsUpIndicatorIcon(@NonNull ActionBar supportActionBar, @NonNull NavigationBarLeftButton leftButton) { if (mFragment.getActivity() != null) { final String iconName = leftButton.getIcon(); @@ -473,34 +456,10 @@ private void updateTitle(@NonNull NavigationBar navigationBar) { } } - private interface OnHomeAsUpClickedCallback { - boolean onHomAsUpClicked(); - } - - private class HomeAsUpClickedCallback implements OnHomeAsUpClickedCallback { - NavigationBarLeftButton mLeftButton; - - HomeAsUpClickedCallback(@NonNull NavigationBarLeftButton button) { - mLeftButton = button; - } - - @Override - public boolean onHomAsUpClicked() { - if (mLeftButton.getId() != null) { - Logger.v(TAG, "HomeAsUpClickedCallback: firing event to React Native, button id: %s", mLeftButton.getId()); - //TODO: This line should be removed with next major version update of ern-navigation. Keeping here for backward compatibility - EnNavigationApi.events().emitOnNavButtonClick(mLeftButton.getId()); - EnNavigationApi.events().emitNavEvent(new NavEventData.Builder(NavEventType.BUTTON_CLICK.toString()).viewId(getMiniAppViewIdentifier()).jsonPayload("{\"id\": \"" + mLeftButton.getId() + "\"}").build()); - return true; - } else if (mLeftButton.getDisabled() != null && mLeftButton.getDisabled()) { - //Back press disabled. - return true; - } - return false; - } - } - private class BackPressedCallback extends OnBackPressedCallback { + @Nullable + private NavigationBarLeftButton mLeftButton; + /** * Create a {@link OnBackPressedCallback}. * @@ -511,15 +470,31 @@ private BackPressedCallback(boolean enabled) { super(enabled); } + private void setEnabled(@Nullable NavigationBarLeftButton leftButton) { + mLeftButton = leftButton; + if (leftButton != null && (leftButton.getId() != null || (leftButton.getDisabled() != null && leftButton.getDisabled()))) { + setEnabled(true); + } else { + setEnabled(false); + } + } + @Override public void handleOnBackPressed() { - if (mOnHomeAsUpClickedCallback == null) { - throw new IllegalStateException("Should never reach here. OnBackPressedCallback should only be enabled if the mOnHomeAsUpClickedCallback is set by React Native component via NavigationBarLeftButton"); + if (mLeftButton == null) { + throw new IllegalStateException("handleOnBackPressed: Should never reach here. NavigationBarLeftButton is null."); } - if (mOnHomeAsUpClickedCallback.onHomAsUpClicked()) { - Logger.v(TAG, "BackPressedCallback, handled by %s", getReactComponentName()); + + if (mLeftButton.getId() != null) { + Logger.v(TAG, "handleOnBackPressed: firing event to React Native, button id: %s", mLeftButton.getId()); + //TODO: This line should be removed with next major version update of ern-navigation. Keeping here for backward compatibility + EnNavigationApi.events().emitOnNavButtonClick(mLeftButton.getId()); + + EnNavigationApi.events().emitNavEvent(new NavEventData.Builder(NavEventType.BUTTON_CLICK.toString()).viewId(getMiniAppViewIdentifier()).jsonPayload("{\"id\": \"" + mLeftButton.getId() + "\"}").build()); + } else if (mLeftButton.getDisabled() != null && mLeftButton.getDisabled()) { + Logger.v(TAG, "handleOnBackPressed: Back press disabled, component:%s", getReactComponentName()); } else { - Logger.w(TAG, "BackPressedCallback, not handled by %s.", getReactComponentName()); + Logger.w(TAG, "handleOnBackPressed: not handled by %s. [SHOULD NEVER REACH HERE]", getReactComponentName()); } } }