1

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:

  1. start app : for example north is on top (marine-compass does not point to north in the link. It's only a example)
  2. rotate your phone to point on other location, for example South.
  3. close your app (by multi-task, swipe or task manager,...)
  4. rotate again your phone to point on other location, for example West
  5. 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"
/>

L-Bo
  • 11
  • 2
  • You need to clear data programmatically see here: http://stackoverflow.com/questions/6134103/clear-applications-data-programmatically – Najeeb Idrees Nov 15 '16 at 14:10
  • Thanks but where to call a clearApplicationData method ? It's not possible to catch an event if user close application from multi-task, swipe. Application just stopped without any events. – L-Bo Nov 15 '16 at 19:30

1 Answers1

0

call it in onStop() method and put your onCreate code in onResume() method

Najeeb Idrees
  • 453
  • 6
  • 15
  • Hi Najeeb, I try to call method to clear data in onStop() method (I use this method from this link https://www.hrupin.com/2011/11/how-to-clear-user-data-in-your-android-application-programmatically), and place create code in onResume() but doesn't work. No changes. – L-Bo Nov 16 '16 at 15:11