Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash during composable attachment to android.app.Activity #613

Merged
merged 3 commits into from
Feb 27, 2024

Conversation

prudrabhat
Copy link
Contributor

@prudrabhat prudrabhat commented Feb 25, 2024

Description

ComposeView requires a lifecycle owner when attaching to a view and not finding one will result in a crash.
android.app.Activity, which is the base class in the framework package, does not provide a lifecycle owner by default.
android.app.Activty has not been updated for a long time and Android created AppCompatActivity/FragmentActivity in the support package to allow updates to be compatible with old android devices. Android recommends using inheriting from AppCompatActivity or similar which provides a lifecycle owner by default (irrespective of whether compose is being used or not).

There may be cases where apps still use this legacy Activity in their apps and launching our Ui Service components on top of them will not work unless the ComposeView sees that there is some lifecycle owner.
To fix this, attach a proxy lifecycle owner and related owners to the view of the activity just before attaching the ComposeView and detach it when the activity is destroyed. This will allow displaying out UI components like InAppMessages, Floating buttons on these legacy implementations. Since there is originally no observer attachable to the Activity, there are no components dependent on the events from this owner except for the compose view.

Related Issue

Motivation and Context

Fix crash during composable attachment to android.app.Activity

How Has This Been Tested?

  • Manual tests by launching an android.app.Activity with test app and also using Assurance 2.x which uses the same.. Unit tests need complicated logic due to the way underlying implementation of the View.find*Owner.

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • I have signed the Adobe Open Source CLA.
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

android.app.Activity, which is the base class in the framework package,
does not provide a lifecycle owner by default.

ComposeView requires a lifecycle owner  when attaching to a view and
not finding one will result in a crash. android.app.Activty has not been
updated for a long time and Android created AppCompatActivity/Fragment activity
in the support package to allow updates to be compatible with old android
devices. Android recommends using inheriting from AppCompatActivity or
similar which provides a lifecycle owner by default (irrespective of whether
compose is being used or not).

There may be cases where apps use this legacy Activity in their apps and launching
our Ui Service components on top of them will not work unless the ComposeView sees that
there is some lifecycle owner.
To fix this, attach a proxy lifecycle owner and related owners
to the view of the activity just before attaching the ComposeView and detach it when
the activity is destroyed. This will allow displaying out UI components like InAppMessages,
Floating buttons on these legacy implementations.

Since there is originally no observer attachable to the Activity, there are no components
dependent on the events from this owner except for the compose view.
Copy link

codecov bot commented Feb 25, 2024

Codecov Report

Merging #613 (7f6a1d7) into feature/uiservice (fafc9a9) will decrease coverage by 0.37%.
The diff coverage is 10.64%.

Additional details and impacted files
@@                   Coverage Diff                   @@
##             feature/uiservice     #613      +/-   ##
=======================================================
- Coverage                81.52%   81.14%   -0.37%     
  Complexity                2110     2110              
=======================================================
  Files                      189      190       +1     
  Lines                     8852     8899      +47     
  Branches                  1108     1110       +2     
=======================================================
+ Hits                      7216     7221       +5     
- Misses                    1078     1120      +42     
  Partials                   558      558              
Flag Coverage Δ
functional-tests 27.37% <0.00%> (-0.15%) ⬇️
unit-tests 68.68% <10.64%> (-0.31%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
...keting/mobile/services/ui/common/AEPPresentable.kt 94.16% <100.00%> (+0.16%) ⬆️
...keting/mobile/internal/util/ActivityCompatOwner.kt 2.33% <2.33%> (ø)

view?.apply {
setViewTreeLifecycleOwner(null)
setViewTreeViewModelStoreOwner(null)
setViewTreeSavedStateRegistryOwner(null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you miss clearing setViewTreeOnBackPressedDispatcherOwner?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The backPressedDispatcherOwner will be cleared automatically. ThesetViewTreeOnBackPressedDispatcherOwner does not accept null's.

@prudrabhat prudrabhat merged commit f6528b0 into adobe:feature/uiservice Feb 27, 2024
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants