Search code examples
androidlocal-storageandroid-webviewhybrid-mobile-appwebchromeclient

Android WebView not reading data from local storage in first time page finished?


I am trying to read local storage data from javascript using WebView in android.

Here is my code

 @Override
    public void onPageFinished(WebView view, String url) {
      getLocalStorageAndSaveOnAndroidPref("androidDeliveryAppStickyNotificationTitle", view);
    }


 public static void getLocalStorageAndSaveOnAndroidPref(String key, WebView view) {
        String JAVASCRIPT_LOCAL_STORAGE_LOOKUP = "javascript:window";
        Log.d(TAG, "getLocalStorageAndSaveOnAndroidPref: function called");
        view.evaluateJavascript(JAVASCRIPT_LOCAL_STORAGE_LOOKUP.concat(".localStorage.getItem('" + key + "')"), value -> {
            Log.d(TAG, "getLocalStorageAndSaveOnAndroidPref: "+value);
            SharePref.setDataPref(key, value);
        });
    }

But this is giving me null when it's loaded the first time.

I want to achieve that the local storage data should be loaded the first time the page is finished.

Any idea how to achieve that.


Solution

  • I have a better solution for this it will work for the first time opening WebView.

    you just have to use a custom callback. like this

    public interface DataSavedCallback {
      void onDataSaved();
    }
    
      public static void getLocalStorageAndSaveOnAndroidPref(String[] keys,
    WebView view,dataSavedCallback savedCallback) {
      
        String JAVASCRIPT_LOCAL_STORAGE_LOOKUP = "javascript:window";
         view.evaluateJavascript(JAVASCRIPT_LOCAL_STORAGE_LOOKUP.concat(".localStorage.getItem('" + keys[i] + "')"), new ValueCallback<String>() {
                    @Override
                    public void onReceiveValue(String value) {
                        Log.d(TAG, "getLocalStorageAndSaveOnAndroidPref: " + value);
                    }
                });
    
        }
    

    then use this function to get the value of the key from the webview local storage

    public static void getLocalStorageAndSaveOnAndroidPref(String[] keys,
    WebView view,dataSavedCallback savedCallback) {
      
        String JAVASCRIPT_LOCAL_STORAGE_LOOKUP = "javascript:window";
         view.evaluateJavascript(JAVASCRIPT_LOCAL_STORAGE_LOOKUP.concat(".localStorage.getItem('" + keys[i] + "')"), new ValueCallback<String>() {
                    @Override
                    public void onReceiveValue(String value) {
                        Log.d(TAG, "getLocalStorageAndSaveOnAndroidPref: " + value);
                    }
                });
    
        }
    

    you can call the above function anywhere using your webview

    myWebView.post(() -> {
                String[] keys = {"androidDeliveryAppStickyNotificationTitle","androidDeliveryAppStickyNotificationSubtitle","androidAppNewSoundAlertTitle"};
                getLocalStorageAndSaveOnAndroidPref(keys, myWebView, new dataSavedCallback() {
                    @Override
                    public void onDataSaved() {
                    //do whatever you want
                    }
                });
    
            });