Skip to content

Add invisible FragmentHostActivity to support custom HTML in-app messages on NativeActivity #809

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

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from

Conversation

simonbullen
Copy link

@simonbullen simonbullen commented May 23, 2025

This PR adds support for rendering CleverTap’s fragment-based in-app notifications (e.g. custom HTML) in Unreal Engine projects that use a NativeActivity, which does not support fragments.

Problem

CleverTap’s SDK assumes that the current activity is a FragmentActivity, but Unreal-based Android apps typically use NativeActivity. As a result, certain in-app notification types (notably HTML-based ones) fail to display or cause runtime exceptions.

Solution

Introduced a minimal FragmentHostActivity that subclasses AppCompatActivity and provides a safe fragment container.

Added logic to detect when the current activity is not a FragmentActivity, defer the notification, and launch FragmentHostActivity instead.

Once launched, the host activity proceeds with CleverTap’s regular rendering logic.

This approach isolates fragment-specific logic to a dedicated host, without modifying the behavior for apps already using FragmentActivity.

Summary by CodeRabbit

  • New Features

    • Introduced a new activity to host in-app notifications that require a fragment environment, ensuring smoother display for specific notification types.
    • Added a custom theme for the new activity, providing a transparent, fullscreen, and title-less appearance for hosted notifications.
  • Bug Fixes

    • Improved handling of in-app notifications by deferring their display until a suitable environment is available, preventing display issues.

CTLalit and others added 4 commits March 11, 2025 01:36
Release Core 7.3.0, HMS 1.5.0, Push Templates 1.4.0
…to host the notification when the current activity isn't a FragmentActivity.
Copy link
Contributor

coderabbitai bot commented May 23, 2025

Walkthrough

A new activity, FragmentHostActivity, was introduced to host specific in-app notification types requiring a fragment environment without transition animations. The AndroidManifest and styles were updated to support this activity, and logic was added to InAppController to launch the host activity when needed, deferring notification display until the environment is suitable.

Changes

File(s) Change Summary
clevertap-core/src/main/AndroidManifest.xml Added FragmentHostActivity declaration with custom theme, no history, singleTop launch mode, and not exported.
clevertap-core/src/main/java/com/clevertap/android/sdk/FragmentHostActivity.java Introduced FragmentHostActivity class to host fragments without transition animations; includes static launch and hosting state methods, lifecycle overrides, and fragment lifecycle callbacks.
clevertap-core/src/main/java/com/clevertap/android/sdk/inapp/InAppController.java Updated showInApp to check notification type; launches FragmentHostActivity if needed and re-queues notifications. Added helper method for type checking.
clevertap-core/src/main/res/values/styles.xml Added CleverTapFragmentHostTheme style for transparent, fullscreen, no-title, and translucent window configuration.

Sequence Diagram(s)

sequenceDiagram
    participant AppActivity as App Activity
    participant InAppController
    participant FragmentHostActivity

    AppActivity->>InAppController: showInApp(notification)
    InAppController->>InAppController: requiresFragmentHost(type)?
    alt Requires Fragment Host & Not in FragmentActivity
        InAppController->>InAppController: Re-queue notification
        InAppController->>FragmentHostActivity: launch(current)
        Note right of FragmentHostActivity: Activity starts with no animation
        FragmentHostActivity->>FragmentHostActivity: onCreate/onResume (set hosting)
        FragmentHostActivity->>FragmentHostActivity: Register fragment lifecycle callbacks
    else
        InAppController->>AppActivity: Display notification
    end
Loading

Note

⚡️ AI Code Reviews for VS Code, Cursor, Windsurf

CodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback.
Learn more here.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c0c1e84 and 0f0ca5b.

📒 Files selected for processing (1)
  • clevertap-core/src/main/java/com/clevertap/android/sdk/FragmentHostActivity.java (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • clevertap-core/src/main/java/com/clevertap/android/sdk/FragmentHostActivity.java
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🧹 Nitpick comments (3)
clevertap-core/src/main/AndroidManifest.xml (1)

18-23: Consider adding configChanges attribute for consistency.

The activity declaration looks good with appropriate security and launch mode settings. However, consider adding a configChanges attribute similar to other activities in the manifest to handle device configuration changes properly:

 <activity
     android:name="com.clevertap.android.sdk.FragmentHostActivity"
     android:theme="@style/CleverTapFragmentHostTheme"
     android:noHistory="true"
     android:launchMode="singleTop"
+    android:configChanges="keyboardHidden|orientation|screenSize"
     android:exported="false" />

This ensures the activity handles configuration changes gracefully without unnecessary recreation.

clevertap-core/src/main/java/com/clevertap/android/sdk/inapp/InAppController.java (1)

796-815: Fragment host detection logic is well-implemented.

The logic correctly detects when a fragment host is needed and launches FragmentHostActivity when the current activity isn't a FragmentActivity. The approach of re-queuing the notification and deferring display is sound.

However, consider adding error handling for the activity launch:

 if (!FragmentHostActivity.isHosting()) {
-    FragmentHostActivity.launch(act);
+    try {
+        FragmentHostActivity.launch(act);
+    } catch (Exception e) {
+        logger.verbose(config.getAccountId(), 
+            "Failed to launch FragmentHostActivity: " + e.getMessage());
+        // Fallback: try to show notification anyway or skip
+        return;
+    }
 }
clevertap-core/src/main/java/com/clevertap/android/sdk/FragmentHostActivity.java (1)

15-16: Consider thread safety for the static hosting flag.

The static hosting flag could have thread safety issues in concurrent scenarios. Consider using AtomicBoolean for better thread safety:

-private static boolean hosting = false;
-public static boolean isHosting() { return hosting; }
+private static final AtomicBoolean hosting = new AtomicBoolean(false);
+public static boolean isHosting() { return hosting.get(); }

Then update the assignments to use hosting.set(true) and hosting.set(false).

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cc4b305 and a193d30.

📒 Files selected for processing (4)
  • clevertap-core/src/main/AndroidManifest.xml (1 hunks)
  • clevertap-core/src/main/java/com/clevertap/android/sdk/FragmentHostActivity.java (1 hunks)
  • clevertap-core/src/main/java/com/clevertap/android/sdk/inapp/InAppController.java (3 hunks)
  • clevertap-core/src/main/res/values/styles.xml (1 hunks)
🔇 Additional comments (6)
clevertap-core/src/main/res/values/styles.xml (1)

3-8: LGTM! Transparent theme configuration is correct.

The theme configuration properly sets up a transparent, fullscreen overlay activity that will be invisible to users while hosting fragments. All attributes are standard and correctly configured for this use case.

clevertap-core/src/main/java/com/clevertap/android/sdk/inapp/InAppController.java (2)

27-27: LGTM! Import added correctly.

The import for FragmentHostActivity is properly placed and necessary for the new functionality.


897-908: Helper method is well-designed and correctly implements type checking.

The requiresFragmentHost() method correctly identifies the in-app notification types that require fragment hosting. The implementation matches the types handled in the switch statement below.

clevertap-core/src/main/java/com/clevertap/android/sdk/FragmentHostActivity.java (3)

18-25: Launch method implementation is correct.

The static launch method properly creates an intent, adds the FLAG_ACTIVITY_NEW_TASK flag, and suppresses transitions. This approach is appropriate for launching a utility activity from any context.


27-33: onCreate implementation looks good.

The lifecycle management correctly sets the hosting flag and suppresses transitions. The activity doesn't set a content view, which is appropriate since it's meant to be invisible and only host fragments.


51-57: onDestroy implementation is correct.

The lifecycle cleanup properly resets the hosting flag and suppresses transitions. This ensures proper state management when the activity is destroyed.

@CTLalit CTLalit changed the base branch from master to develop May 26, 2025 08:05
Copy link
Collaborator

@CTLalit CTLalit left a comment

Choose a reason for hiding this comment

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

@simonbullen I think this is apply changes to all SDK flows. Consider putting behind a flag which can be provided to core-sdk from Unity (any other third party) and make it configurable?

@simonbullen
Copy link
Author

simonbullen commented May 27, 2025 via email

@CTLalit
Copy link
Collaborator

CTLalit commented May 27, 2025

@simonbullen makes sense, i saw the code of this patch working only when underlying infra does not support fragments (via instanceOf check). We have added this in our docs to use a specific type of activity; but thanks for providing this piece, we will consider getting it checked in after discussion with my team.

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