Skip to content

Commit 076cfbe

Browse files
committed
Improved LiveChatView cleanup
1 parent bc965cf commit 076cfbe

File tree

3 files changed

+75
-10
lines changed

3 files changed

+75
-10
lines changed

app/src/main/java/com/fastcomments/LiveChatExampleActivity.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,18 @@ class LiveChatExampleActivity : AppCompatActivity() {
6363

6464
override fun onResume() {
6565
super.onResume()
66-
// Refresh live events connection when returning to the app
6766
sdk.refreshLiveEvents()
67+
liveChatView.onResume()
68+
}
69+
70+
override fun onPause() {
71+
super.onPause()
72+
liveChatView.onPause()
6873
}
6974

7075
override fun onDestroy() {
7176
super.onDestroy()
72-
// Clean up resources
77+
liveChatView.cleanup()
7378
sdk.cleanup()
7479
}
7580
}

libraries/sdk/src/main/java/com/fastcomments/sdk/FastCommentsSDK.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class FastCommentsSDK {
5353
public String blockingErrorMessage;
5454

5555
private SubscribeToChangesResult liveEventSubscription;
56-
private final LiveEventSubscriber liveEventSubscriber;
56+
private LiveEventSubscriber liveEventSubscriber;
5757
private String tenantIdWS;
5858
private String urlIdWS;
5959
private String userIdWS;
@@ -1134,14 +1134,19 @@ private void handlePresenceChange(LiveEvent eventData) {
11341134
}
11351135
}
11361136

1137-
/**
1138-
* Cleanup resources, including WebSocket connections
1139-
*/
11401137
public void cleanup() {
11411138
if (liveEventSubscription != null) {
11421139
liveEventSubscription.close();
11431140
liveEventSubscription = null;
11441141
}
1142+
1143+
if (liveEventSubscriber != null) {
1144+
liveEventSubscriber = null;
1145+
}
1146+
1147+
tenantIdWS = null;
1148+
urlIdWS = null;
1149+
userIdWS = null;
11451150
}
11461151

11471152
/**

libraries/sdk/src/main/java/com/fastcomments/sdk/LiveChatView.java

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,18 +1524,55 @@ public boolean isAutoScrollToBottom() {
15241524
* Should be called when the view is no longer needed, especially when used in fragments.
15251525
*/
15261526
public void cleanup() {
1527-
// Clear back pressed callback
1527+
stopDateUpdateTimer();
1528+
1529+
if (dateUpdateHandler != null) {
1530+
dateUpdateHandler.removeCallbacksAndMessages(null);
1531+
dateUpdateHandler = null;
1532+
}
1533+
dateUpdateRunnable = null;
1534+
15281535
if (backPressedCallback != null) {
15291536
backPressedCallback.setEnabled(false);
15301537
backPressedCallback = null;
15311538
}
15321539

1533-
// Stop any timers
1534-
stopDateUpdateTimer();
1540+
if (recyclerView != null) {
1541+
recyclerView.clearOnScrollListeners();
1542+
}
1543+
1544+
if (adapter != null) {
1545+
adapter.setRequestingReplyListener(null);
1546+
adapter.setUpVoteListener(null);
1547+
adapter.setDownVoteListener(null);
1548+
adapter.setCommentMenuListener(null);
1549+
adapter.setUserClickListener(null);
1550+
adapter.setGetChildrenProducer(null);
1551+
adapter = null;
1552+
}
1553+
1554+
if (bottomCommentInput != null) {
1555+
bottomCommentInput.setOnCommentSubmitListener(null);
1556+
bottomCommentInput.setOnReplyStateChangeListener(null);
1557+
}
1558+
if (commentForm != null) {
1559+
commentForm.setOnCommentSubmitListener(null);
1560+
commentForm.setOnCancelReplyListener(null);
1561+
}
1562+
1563+
if (btnNextComments != null) {
1564+
btnNextComments.setOnClickListener(null);
1565+
}
1566+
if (btnLoadAll != null) {
1567+
btnLoadAll.setOnClickListener(null);
1568+
}
1569+
1570+
commentActionListener = null;
1571+
userClickListener = null;
15351572

1536-
// Clean up SDK
15371573
if (sdk != null) {
15381574
sdk.cleanup();
1575+
sdk = null;
15391576
}
15401577
}
15411578

@@ -1551,6 +1588,24 @@ public void setBackPressHandlingEnabled(boolean enabled) {
15511588
}
15521589
}
15531590

1591+
/**
1592+
* Lifecycle method to call when the view/fragment is paused.
1593+
* Stops timers and disables back press handling to prevent memory leaks.
1594+
*/
1595+
public void onPause() {
1596+
stopDateUpdateTimer();
1597+
setBackPressHandlingEnabled(false);
1598+
}
1599+
1600+
/**
1601+
* Lifecycle method to call when the view/fragment is resumed.
1602+
* Restarts timers and enables back press handling.
1603+
*/
1604+
public void onResume() {
1605+
setBackPressHandlingEnabled(true);
1606+
startDateUpdateTimer();
1607+
}
1608+
15541609
/**
15551610
* Sets up the demo banner if tenant ID is "demo"
15561611
*/

0 commit comments

Comments
 (0)