Skip to content

Commit bdbb1dd

Browse files
authored
Merge pull request #24 from FastComments/live-chat-backpress
Fixing backpress in live chat not working
2 parents 1ba3aa8 + 4fe7694 commit bdbb1dd

File tree

1 file changed

+47
-7
lines changed

1 file changed

+47
-7
lines changed

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

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,24 @@ public void handleOnBackPressed() {
220220
.setNegativeButton(android.R.string.no, null)
221221
.show();
222222
} else {
223-
// Text is empty, no need for confirmation
223+
// Text is empty, check if we're in a reply state
224+
boolean wasInReplyState = false;
224225
if (bottomCommentInput != null) {
226+
wasInReplyState = bottomCommentInput.getParentComment() != null;
225227
bottomCommentInput.clearReplyState();
226228
} else if (commentForm != null) {
229+
wasInReplyState = commentForm.getParentComment() != null;
227230
commentForm.resetReplyState();
228231
}
232+
233+
// If we weren't in a reply state, allow normal back navigation
234+
if (!wasInReplyState) {
235+
backPressedCallback.setEnabled(false);
236+
if (getContext() instanceof Activity) {
237+
((Activity) getContext()).onBackPressed();
238+
}
239+
backPressedCallback.setEnabled(true);
240+
}
229241
}
230242
}
231243
};
@@ -1485,12 +1497,8 @@ private void updateDates() {
14851497
@Override
14861498
protected void onDetachedFromWindow() {
14871499
super.onDetachedFromWindow();
1488-
// Stop the timer when the view is detached to prevent memory leaks
1489-
stopDateUpdateTimer();
1490-
// Clean up WebSocket connections when the view is detached
1491-
if (sdk != null) {
1492-
sdk.cleanup();
1493-
}
1500+
// Clean up all resources including backpress callback
1501+
cleanup();
14941502
}
14951503

14961504
/**
@@ -1511,6 +1519,38 @@ public boolean isAutoScrollToBottom() {
15111519
return autoScrollToBottom;
15121520
}
15131521

1522+
/**
1523+
* Cleanup method to properly release resources and callbacks.
1524+
* Should be called when the view is no longer needed, especially when used in fragments.
1525+
*/
1526+
public void cleanup() {
1527+
// Clear back pressed callback
1528+
if (backPressedCallback != null) {
1529+
backPressedCallback.setEnabled(false);
1530+
backPressedCallback = null;
1531+
}
1532+
1533+
// Stop any timers
1534+
stopDateUpdateTimer();
1535+
1536+
// Clean up SDK
1537+
if (sdk != null) {
1538+
sdk.cleanup();
1539+
}
1540+
}
1541+
1542+
/**
1543+
* Enable or disable back press handling.
1544+
* Useful for fragment lifecycle management.
1545+
*
1546+
* @param enabled Whether back press handling should be active
1547+
*/
1548+
public void setBackPressHandlingEnabled(boolean enabled) {
1549+
if (backPressedCallback != null) {
1550+
backPressedCallback.setEnabled(enabled);
1551+
}
1552+
}
1553+
15141554
/**
15151555
* Sets up the demo banner if tenant ID is "demo"
15161556
*/

0 commit comments

Comments
 (0)