Nativescript: Webview reloads when coming back on the page

Created on 28 Jul 2017  ยท  13Comments  ยท  Source: NativeScript/NativeScript

I have a NativeScript Page (i.e. main page) that contains an WebView and means to navigate to another NativeScript Page (i.e. about page).

If I got to the about page, and then come back to the main page, on android only, the webview on the main page will reload with the initial URL.

The fact that on iOS it works as expected leads me to believe it's a bug.

I have this problem on two different applications that I'm working on now, and I also found another guy on StackOverflow with the same issue. If it is needed, I will also write a small example app that will illustrate the issue.

I'm using {N} with vanilla JS.

$ tns info
All NativeScript components versions information
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Component        โ”‚ Current version โ”‚ Latest version โ”‚ Information      โ”‚
โ”‚ nativescript     โ”‚ 3.1.2           โ”‚ 3.1.3          โ”‚ Update available โ”‚
โ”‚ tns-core-modules โ”‚ 3.1.0           โ”‚ 3.1.0          โ”‚ Up to date       โ”‚
โ”‚ tns-android      โ”‚ 3.1.1           โ”‚ 3.1.1          โ”‚ Up to date       โ”‚
โ”‚ tns-ios          โ”‚ 3.1.0           โ”‚ 3.1.0          โ”‚ Up to date       โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜



android

Most helpful comment

@surdu
I think you can use npm i tns-core-modules@next --save instead of tns plugin add.

All 13 comments

Hi @surdu,
I research this issue and it seems that this is something specific to the Android platform. I found this thread in StackOverflow where it is suggested to set android:launchMode="singleInstance" in your androidmanifest.xml, which will prevent reloading the WebView page.

Hello @tsonevn,

Nope, that doesn't solve this problem. ๐Ÿ˜ข

Also, the problem described in that SO question is not reproducible on my apps. In my case, I don't leave the app, just move to another screen (I don't think it's even another activity ?)

Archive.zip
Hi @surdu,
I reviewed further this case and was able to reproduce similar scenario and found that this happens in the cases when you setup the URL for the WebView on navigatingTo event.
In this case, the URL will be reset every time when you navigating back from the previous page.

To resolve this you could setup the URL to the Observable outside the event, which will prevent resetting the page. For example:

import { EventData, Observable } from 'data/observable';
import { Page } from 'ui/page';
import {topmost} from "ui/frame"
import { HelloWorldModel } from './main-view-model';
var tmpObservable= new Observable();
tmpObservable.set("wurl", "https://google.com");
// Event handler for Page "navigatingTo" event attached in main-page.xml
export function navigatingTo(args: EventData) {

    let page = <Page>args.object;
    console.log("navigating to ")

    page.bindingContext = tmpObservable;
}
export function loadPage(){
    tmpObservable.set("wurl", "https://docs.nativescript.org");
}

export function onTap(){
    topmost().navigate("page2")
}

I am also attaching a sample project.

If your scenario is different please provide a sample project.

Hi @surdu,
Were you able to resolve your case with my latest suggestion?

@tsonevn Sorry, managed now to squeeze some time to test it.

In you code, you can change:
<WebView height="200" src="{{wurl}}" />
into:
<WebView height="200" src="https://docs.nativescript.org" />

and the problem is still reproducible.

Hi @surdu,
Thank you for your further investigation.
Indeed while setting the value for the src property inline in the XML the WebView page will be reloaded every time, when you navigate back to the previous page. This is caused due to fact that the native WebView will be destroyed and recreated every time when you navigate to the page with WebView.

However, this behavior has been fixed in @next modules and you could test it on your side while following the described steps:

tns plugin remove tns-core-modules

tns plugin add tns-core-modules@next

also before rebuild delete node_modules and platforms folders

@tsonevn

$ tns plugin add tns-core-modules@next
[...]

npm WARN [email protected] requires a peer of tns-core-modules@^3.0.0 ||
 ^3.0.0-rc.1 || >=3.0.0-2017 but none was installed.

Command npm install tns-core-modules@next --save failed with exit code 1

I've included that warning, but I don't believe that's the issue, as I get that warning also for tns-core-modules 3.1 and it works just fine.

Any idea how can I make the tns plugin add command more verbose in order to see what the problem might be ?

@surdu
I think you can use npm i tns-core-modules@next --save instead of tns plugin add.

Works! Thanks a lot !

hiii @tsonevn and @surdu, i have the same problem but i i am using react native webview..
can you tell me how to solve this problem in react native webview

@abbas5454 This should have been already fixed. The only thing I did then was update tns-core-modules@next, but the fix should be live now in the latest NativeScript version.

Hi @abbas5454,
If you are facing an issue, while using nativescript-advanced-webview plugin, I would suggest contacting the authors

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

OscarLopezArnaiz picture OscarLopezArnaiz  ยท  3Comments

rogangriffin picture rogangriffin  ยท  3Comments

guillaume-roy picture guillaume-roy  ยท  3Comments

kn9ts picture kn9ts  ยท  3Comments

NordlingDev picture NordlingDev  ยท  3Comments