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

Attach lifecycle listeners internally in initialize method #131

Merged
merged 4 commits into from
Feb 20, 2024

Conversation

evan-masseau
Copy link
Contributor

Description

While working on a ticket to automatically attach android lifecycle listeners for react native SDK, I've realized we could extend that convenience to the android SDK.

Check List

  • Are you changing anything with the public API? YES - marked lifecycleCallbacks deprecated
  • Are your changes backwards compatible with previous SDK Versions? YES
  • Have you tested this change on real device? YES
  • Have you added unit test coverage for your changes? Yes
  • Have you verified that your changes are compatible with all Android versions the SDK currently supports? - YES

Changelog / Code Overview

  • Added lifecycle listeners in the initialize method instead of requiring app developers to do it.
  • Mark the public property to be deprecated and replace with a no-op implementation to avoid duplicate listeners.
  • Update readme instructions, migration guide.

Test Plan

  • Updated unit test
  • Verify lifecycle listeners working in android test app
  • Pull this code into the React Native SDK to make sure it works there too

Related Issues/Tickets

CHNL-4635

Mark the public property to be deprecated and replace with a no-op implementation to avoid duplicate listeners.
Update readme instructions, migration guide.
@evan-masseau evan-masseau changed the title Internalize the lifecycle listeners Attach lifecycle listeners internally in initialize method Feb 15, 2024
* and prevent duplicate registration of the KlaviyoLifecycleMonitor
* until next major release when we can make the breaking change to remove that public property
*/
object NoOpLifecycleCallbacks : Application.ActivityLifecycleCallbacks {
Copy link
Contributor

Choose a reason for hiding this comment

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

So this would also be removed in next major release once we no longer need to worry about duplicate lifecycle registration?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct, I just can't remove it in a minor release by semantic versioning rules

```kotlin
registerActivityLifecycleCallbacks(Klaviyo.lifecycleCallbacks)
```
For version 2.1.x, `Klaviyo.lifecycleCallbacks` has been replaced with a no-op implementation to avoid duplicative
Copy link
Contributor

Choose a reason for hiding this comment

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

💯

application?.apply {
unregisterActivityLifecycleCallbacks(Registry.lifecycleCallbacks)
registerActivityLifecycleCallbacks(Registry.lifecycleCallbacks)
} ?: throw LifecycleException()
Copy link
Contributor

Choose a reason for hiding this comment

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

Would this crash their app? Should it log a warning instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep! It will log a warning when #129 and #130 are merged

Copy link
Contributor

Choose a reason for hiding this comment

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

oh nice!

@@ -47,6 +52,12 @@ object Klaviyo {
.applicationContext(applicationContext)
.build()
)

val application = applicationContext.applicationContext as? Application
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here is the documentation on applicationContext

In earlier speccing last year, I shied away from using this method to access the Application object because of a stackoverflow post from 2011 that warned off using it. Now that I've read more about it, I think that post was incorrect. For example, this description from official docs describes exactly what we're doing here:

Return the context of the single, global Application object of the current process. This generally should only be used if you need a Context whose lifecycle is separate from the current context, that is tied to the lifetime of the process rather than the current component.

Evan Masseau added 2 commits February 20, 2024 16:11
…ecm/lifecycle

# Conflicts:
#	sdk/analytics/src/main/java/com/klaviyo/analytics/Klaviyo.kt
@evan-masseau evan-masseau merged commit e9598b0 into master Feb 20, 2024
2 checks passed
@evan-masseau evan-masseau deleted the ecm/lifecycle branch February 20, 2024 21:22
evan-masseau added a commit that referenced this pull request Feb 21, 2024
…ze method (#131)* Internalize the lifecycle listener attachment.* Mark the public property to be deprecated and replace with a no-op implementation to avoid duplicate listeners.* Update readme instructions, migration guide.---------Co-authored-by: Evan Masseau <>" (#136)

* Auto generate documentation PRs (#124)

Automate all the things

Co-authored-by: Evan Masseau <>

* Minor README and Documentation Updates (#121)

* Fixed SDK name in license section
* Fixed appearance of some of the advanced section after seeing it on the dokka html
* Other grammar/spelling fixes

---------

Co-authored-by: Evan Masseau <>

* Add note about anonymous profiles to readme (#127)


---------

Co-authored-by: Evan Masseau <>

* Wrap public methods in try/catch (#129)

Added safety checks around public API methods to prevent throwing exceptions that would otherwise crash the host application

---------

Co-authored-by: Evan Masseau <>

* Logging (#130)

- Enable logging in published SDK
- Added level-aware logging with a configurable threshold from code or manifest file.
- Added warning level
- Threshold has a default based on debug/release, can be configured from code and from manifest
- Updated tests, removed some old log related test code from earlier build variants/flavors
- Adjusted log levels throughout the code

---------

Co-authored-by: Evan Masseau <>

* Attach lifecycle listeners internally in initialize method (#131)

* Internalize the lifecycle listener attachment.
* Mark the public property to be deprecated and replace with a no-op implementation to avoid duplicate listeners.
* Update readme instructions, migration guide.

---------

Co-authored-by: Evan Masseau <>

* Generated docs for e9598b0

---------

Co-authored-by: Evan C Masseau <5167687+evan-masseau@users.noreply.github.com>
Co-authored-by: github-actions <github-actions@github.com>
evan-masseau pushed a commit that referenced this pull request Mar 18, 2024
…ation

* commit 'b12ef65b0d460b343c4d1eb2bac6e227a6f51d62':
  Release Version 2.1.0 (#137)
  Attach lifecycle listeners internally in initialize method (#131)
  Logging (#130)
  Wrap public methods in try/catch (#129)
  Add note about anonymous profiles to readme (#127)
  Minor README and Documentation Updates (#121)
  Auto generate documentation PRs (#124)
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.

4 participants