Reputation: 12861
Currently I am using WebView
in my application to load Webpage. I have loaded following webpage URL in WebView
:
http://domain_name/app/
If User is not logged in then URL is redirected to another URL which is login URL. here is login URL:
http://domain_name/app/index.php?r=site/login
After User enter username and password it is redirected to following URL:
http://domain_name/app/index.php?r=site/homepage
When User successfully logged in then homepage is shown to user.
My Problem is that when application is removed from Recent list login data is lost and application is redirected to Login screen.
I have one way to solve this issue. First is to give Login page URL initially after successful user login I change URL to Homepage URL instead of Login page URL.
Is there any other way to solve this issue or any way to save login data into application storage?
Upvotes: 2
Views: 6598
Reputation: 12861
I am answering my own question I thought it might be helpful to others.
Finally I have solve my problem through Caching in Internal and External Storage(Save data to External Storage if available)
First I have created custom Application
class which creates cache storage path in internal or external storage.
MyApplication.java
import android.app.Application;
import android.os.Environment;
import java.io.File;
public class MyApplication extends Application {
protected File extStorageAppBasePath;
protected File extStorageAppCachePath;
@Override
public void onCreate() {
super.onCreate();
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
File externalStorageDir = Environment.getExternalStorageDirectory();
if (externalStorageDir != null) {
extStorageAppBasePath = new File(externalStorageDir.getAbsolutePath() +
File.separator + "Android" + File.separator + "data" +
File.separator + getPackageName());
}
if (extStorageAppBasePath != null) {
extStorageAppCachePath = new File(extStorageAppBasePath.getAbsolutePath() +
File.separator + "cache");
boolean isCachePathAvailable = true;
if (!extStorageAppCachePath.exists()) {
isCachePathAvailable = extStorageAppCachePath.mkdirs();
}
if (!isCachePathAvailable) {
extStorageAppCachePath = null;
}
}
}
}
@Override
public File getCacheDir() {
if (extStorageAppCachePath != null) {
return extStorageAppCachePath;
}
else {
return super.getCacheDir();
}
}
}
In Android Manifest file I have given following permission and Application name under application
tag.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:name=".MyApplication"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
........................................
</application>
Then final enable cache to load from storage if cache is available.
mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
Upvotes: 3
Reputation: 24991
Before load the page use:
CookieManager.getInstance().setAcceptCookie(true);
if it won't work read about CookieSyncManager to synchronize the browser cookie store in RAM and permanent storage.
For Lollipop and above this should be enough:
CookieManager.getInstance().setAcceptThirdPartyCookies(true);
Upvotes: 1