18
18
import org .json .JSONObject ;
19
19
20
20
import java .io .UnsupportedEncodingException ;
21
- import java .lang .ref .WeakReference ;
22
21
23
22
import static com .onesignal .OSViewUtils .dpToPx ;
24
23
@@ -64,7 +63,8 @@ boolean isBanner() {
64
63
@ NonNull private Activity activity ;
65
64
@ NonNull private OSInAppMessage message ;
66
65
67
- private boolean firstShow = true ;
66
+ private String currentActivityName = null ;
67
+ private Integer lastPageHeight = null ;
68
68
69
69
interface OneSignalGenericCallback {
70
70
void onComplete ();
@@ -275,40 +275,49 @@ private void calculateHeightAndShowWebViewAfterNewActivity() {
275
275
276
276
OneSignal .Log (OneSignal .LOG_LEVEL .DEBUG , "In app message new activity, calculate height and show " );
277
277
278
- // Using post to ensure that the status bar inset is already added to the view
279
- OSViewUtils .decorViewReady (activity , new Runnable () {
280
- @ Override
281
- public void run () {
282
- // At time point the webView isn't attached to a view
283
- // Set the WebView to the max screen size then run JS to evaluate the height.
284
- setWebViewToMaxSize (activity );
285
- webView .evaluateJavascript (OSJavaScriptInterface .GET_PAGE_META_DATA_JS_FUNCTION , new ValueCallback <String >() {
286
- @ Override
287
- public void onReceiveValue (final String value ) {
288
- try {
289
- int pagePxHeight = pageRectToViewHeight (activity , new JSONObject (value ));
290
- showMessageView (pagePxHeight );
291
- } catch (JSONException e ) {
292
- e .printStackTrace ();
278
+ // Using post to ensure that the status bar inset is already added to the view
279
+ OSViewUtils .decorViewReady (activity , new Runnable () {
280
+ @ Override
281
+ public void run () {
282
+ // At time point the webView isn't attached to a view
283
+ // Set the WebView to the max screen size then run JS to evaluate the height.
284
+ setWebViewToMaxSize (activity );
285
+ webView .evaluateJavascript (OSJavaScriptInterface .GET_PAGE_META_DATA_JS_FUNCTION , new ValueCallback <String >() {
286
+ @ Override
287
+ public void onReceiveValue (final String value ) {
288
+ try {
289
+ int pagePxHeight = pageRectToViewHeight (activity , new JSONObject (value ));
290
+ showMessageView (pagePxHeight );
291
+ } catch (JSONException e ) {
292
+ e .printStackTrace ();
293
+ }
293
294
}
294
- }
295
- });
296
- }
295
+ });
296
+ }
297
297
});
298
298
}
299
299
300
300
@ Override
301
301
void available (final @ NonNull Activity activity ) {
302
+ String lastActivityName = this .currentActivityName ;
302
303
this .activity = activity ;
303
- if (firstShow )
304
+ this .currentActivityName = activity .getLocalClassName ();
305
+
306
+ if (lastActivityName == null )
304
307
showMessageView (null );
305
- else
308
+ else if (!lastActivityName .equals (currentActivityName )) {
309
+ // Navigate to new activity while displaying current IAM
310
+ if (messageView != null )
311
+ messageView .removeAllViews ();
312
+ showMessageView (lastPageHeight );
313
+ } else
306
314
calculateHeightAndShowWebViewAfterNewActivity ();
307
315
}
308
316
309
317
@ Override
310
- void stopped () {
311
- if (messageView != null )
318
+ void stopped (Activity activity ) {
319
+ OneSignal .Log (OneSignal .LOG_LEVEL .DEBUG , "In app message activity stopped, cleaning views" );
320
+ if (messageView != null && currentActivityName .equals (activity .getLocalClassName ()))
312
321
messageView .removeAllViews ();
313
322
}
314
323
@@ -327,8 +336,10 @@ private void showMessageView(@Nullable Integer newHeight) {
327
336
328
337
OneSignal .Log (OneSignal .LOG_LEVEL .DEBUG , "In app message, showing first one with height: " + newHeight );
329
338
messageView .setWebView (webView );
330
- if (newHeight != null )
339
+ if (newHeight != null ) {
340
+ lastPageHeight = newHeight ;
331
341
messageView .updateHeight (newHeight );
342
+ }
332
343
messageView .showView (activity );
333
344
messageView .checkIfShouldDismiss ();
334
345
}
@@ -377,11 +388,11 @@ private void setWebViewToMaxSize(Activity activity) {
377
388
}
378
389
379
390
private void createNewInAppMessageView (@ NonNull Position displayLocation , int pageHeight , boolean dragToDismissDisabled ) {
391
+ lastPageHeight = pageHeight ;
380
392
messageView = new InAppMessageView (webView , displayLocation , pageHeight , message .getDisplayDuration (), dragToDismissDisabled );
381
393
messageView .setMessageController (new InAppMessageView .InAppMessageViewListener () {
382
394
@ Override
383
395
public void onMessageWasShown () {
384
- firstShow = false ;
385
396
OneSignal .getInAppMessageController ().onMessageWasShown (message );
386
397
}
387
398
0 commit comments