I'm trying to implement the Full-Tilt (https://github.com/adtile/Full-Tilt) compass example (https://richtr.github.io/Marine-Compass) in a WebView.
My problem is when you restart application, after closing it (by swipe, or multitasks, ...) the north position for compass is not reset and last position retained.
How to reproduce:
- start app : for example north is on top (marine-compass does not point to north in the link. It's only a example)
- rotate your phone to point on other location, for example South.
- close your app (by multi-task, swipe or task manager,...)
- rotate again your phone to point on other location, for example West
- re-start app : compass point to last position, South for this example. Instead of resetting and point to real position. West in our example.
I try a lot of solutions found on forums, but nothing works. Like
- clearView()
- clearHistory()
- destroy()
- clearCache()
- clearFormData()
- clear cookies
- setCacheMode(WebSettings.LOAD_NO_CACHE)
- setDomStorageEnabled(false)
- setAppCacheEnabled(false)
- loadUrl("about:blank")
Has someone any ideas to solve this point ?
Find below code example...
MainActivity.java :
public class MainActivity extends AppCompatActivity {
//fields
private final String mViewUrl = "https://richtr.github.io/Marine-Compass";
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = ((WebView) findViewById(R.id.testView));
mWebView.loadUrl("about:blank");
String userAgent = "Mozilla/5.0 (Linux; Android 6.0; Android SDK built for x86 Build/MASTER; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/44.0.2403.119 Mobile Safari/537.36";
mWebView.getSettings().setUserAgentString(userAgent);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebView.getSettings().setAppCacheEnabled(false);
mWebView.getSettings().setDomStorageEnabled(false);
mWebView.clearFormData();
mWebView.clearHistory();
mWebView.clearCache(true);
this.clearCookies(this);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient());
mWebView.loadUrl(mViewUrl);
}
@SuppressWarnings("deprecation")
public static void clearCookies(Context context)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
CookieManager.getInstance().removeAllCookies(null);
CookieManager.getInstance().flush();
} else
{
CookieSyncManager cookieSyncMngr=CookieSyncManager.createInstance(context);
cookieSyncMngr.startSync();
CookieManager cookieManager=CookieManager.getInstance();
cookieManager.removeAllCookie();
cookieManager.removeSessionCookie();
cookieSyncMngr.stopSync();
cookieSyncMngr.sync();
}
}
}
AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kedros.test">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.kedros.test.MainActivity">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/testView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>