Coder Social home page Coder Social logo

androidlocalstorage's Introduction

AndroidLocalStorage

LocalStorage is partially broken in Android webviews : Local storage is supposed to be a persistent storage available accros all tabs (or windows) of a browser. On Android, LocalStorage works well but only in the current webview. Multiple webviews of the same app can't share the same data with LocalStorage. That is sad !

JavaScriptInterface to the rescue !

This sample Android app shows you how you can fix the webview LocalStorage with a JavaScriptInterface.

What we do here is :

  • Create an Android database with a simple key/value table
  • Create a JavaScriptInterface which implements all LocalStorage's methods (getItem, setItem, removeItem, clear)
  • Add it to the webview
  • Replace the JavaScript LocalStorage with this "new one" so that you don't have to change the way you use your localStorage usually.

How to use it in your own project ?

  • Include the LocalStorage.java
  • Create the JavaScriptInterface and add it to the webview as shown in MainFragment.java
  • Bind the JavaScript LocalStorage to the new one as shown in assets/index.html

This last step is as simple as this :

<script type="text/javascript">
        try{
            //we replace default localStorage with our Android Database one
            window.localStorage=LocalStorage;    
        }catch(e){
            //LocalStorage class was not found. be sure to add it to the webview
		         	alert("LocalStorage ERROR : can't find android class LocalStorage. switching to raw localStorage")		        
		      }
    //then use your localStorage as usual
    localStorage.setItem('foo','it works')
    alert(localStorage.getItem('foo'))
    
</script>

Play with this example project to see how it works.

androidlocalstorage's People

Contributors

ggendre avatar didimoo avatar ylian avatar

Watchers

Dong4Am avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.