Skip to content Skip to sidebar Skip to footer

How To Refresh/update Serialize Java Object In Sharedpreferences

For storing the serilaizable java object i m using below code if(sharedpreferences.getString(ComplexObjectXMl,'')!=null) { Serializer serializer = new Persister(

Solution 1:

you can compare byte array of those object. one coming from shared preference and other which is latest.

byte[] array = new Gson().toJson(latestObject).getBytes(); //your lattest byte arraybyte[] secondArray = new Gson().toJson(objectConvertedFromSharedPreferenceOLD).getBytes();
    if (Arrays.equals(array, secondArray))

        System.out.println("They are same");
else
 System.out.println("Nope...");

and as you said you can use service or shared preference to check hourly update in oncreateView() but it only happens when user open your app(between hour it will call api for list items)

SharedPreferencesmSettings= PreferenceManager.getDefaultSharedPreferences
                (Dashboard.this);
        longlastUpdateTime= mSettings.getLong("lastUpdateTime", 0);
        /* Should Activity Check for Updates Now? */if ((lastUpdateTime + (3600000)) < System.currentTimeMillis()) {

        /* Save current timestamp for next Check*/
            SharedPreferences.Editoreditor= mSettings.edit();
            lastUpdateTime = System.currentTimeMillis();
            editor.putLong("lastUpdateTime", lastUpdateTime);
            editor.commit();

        /* Start Update for listview your URL to fetch data and then you can check and compare object 
also you can use swipeRefreshLayout so user can also refresh data*///asyncRequestTime.execute(URL);
        } 

Post a Comment for "How To Refresh/update Serialize Java Object In Sharedpreferences"