1

I have an android application with a Webview embedded and a website that this application connects to. When logging in (or logging out) to the user the session data seems to take about a minute or two before it is stored in permanent memory.

The result of this is that if after login (or logout) the app is immediately turned off then the session data does not become persistent and opening the app again later reverts to the previous session.

This looks like some kind of a caching issue, I've looked around for a solution but I can't seem to be able to find any way to force update session data, I would be grateful if someone could point me in the right direction.

2 Answers2

0

Working example:

  public class SessionManager {

    public void setPreferences(Context context, String key, String value) {

        SharedPreferences.Editor editor = context.getSharedPreferences("Androidwarriors", Context.MODE_PRIVATE).edit();
        editor.putString(key, value);
        editor.commit();

    }


    public String getPreferences(Context context, String key) {

        SharedPreferences prefs = context.getSharedPreferences("Androidwarriors", Context.MODE_PRIVATE);
        String position = prefs.getString(key, "");
        return position;
    }
}

For example the splash screen, one screen of the app

public class Splash extends AppCompatActivity {
SessionManager manager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    manager=new SessionManager();

    // METHOD 1

    /****** Create Thread that will sleep for 3 seconds *************/
    Thread background = new Thread() {
        public void run() {

            try {
                // Thread will sleep for 3 seconds
                sleep(3*1000);

                // After 5 seconds redirect to another intent
                String status=manager.getPreferences(Splash.this,"status");
                Log.d("status",status);
                if (status.equals("1")){
                    Intent i=new Intent(Splash.this,MainActivity.class);
                    startActivity(i);
                }else{
                    Intent i=new Intent(Splash.this,LoginActivity.class);
                    startActivity(i);
                }


                //Remove activity
                finish();

            } catch (Exception e) {

            }
        }
    };

    // start thread
    background.start();


}

@Override
public void onBackPressed() {
    super.onBackPressed();
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//***Change Here***
    startActivity(intent);
    finish();
    System.exit(0);
}
}

Add to the login activity

 manager.setPreferences(LoginActivity.this, "status", "1");
    String status=manager.getPreferences(LoginActivity.this,"status");
    Log.d("status", status);
    // Toast.makeText(getBaseContext(), "Bem-Vindo", Toast.LENGTH_LONG).show();
     Intent i = new Intent(LoginActivity.this, Web.class);
    startActivity(i);

on the webview class

public class Web extends AppCompatActivity {
//private WebView webView;

SessionManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web_view);

    manager = new SessionManager();



   // webView = (WebView) findViewById(R.id.webView);
    WebView webView = new WebView(this);
    final WebSettings settings = webView.getSettings();
    webView.getSettings().setJavaScriptEnabled(true);
    settings.setJavaScriptEnabled(true);
    settings.setAppCacheEnabled(false);
    settings.setPluginState(WebSettings.PluginState.ON);
    setContentView(webView);
    webView.setWebViewClient(new WebViewClient());
    webView.loadUrl("link");

}




@Override
public void onBackPressed() {
    super.onBackPressed();

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//***Change Here***
    startActivity(intent);
    finish();
    System.exit(0);
}

}

To close session use this

  manager.setPreferences(Web.this, "status", "0");
            finish();
Kevin Dias
  • 1,043
  • 10
  • 28
  • I don't believe this is relevant. Perhaps I didn't make myself clear, other then the main activity (with the webview) I have no other activities, all of the login and session handling is done by the website the App does not deal with any of that and it is not meant to. The session data is already stored by the webview I just need to know how so that I could force save it myself. – Dovydas Rupšys Nov 23 '16 at 09:23
  • I believe this can help u http://stackoverflow.com/questions/2566485/webview-and-cookies-on-android – Kevin Dias Nov 23 '16 at 17:31
0

I've ended up using the following code in my JavascriptInterface

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        //Not sure if this part works haven't tested it yet
        CookieSyncManager sm = CookieSyncManager.createInstance(context);
        sm.sync();
    } else {
        cookies.flush();
    }

CookieSyncManager is obsolete in lollipop, call flush on CookieManager instance instead. This will save cookies in memory to permanent storage. Note that this is still done asynchronously therefore you can't run it onDestroy() it still takes a couple of seconds.

I've put this in the @JavascriptInterface function which is called by the javascript whenever user logs in or logs out and it seems to work well enough.