0

i put a webview in an activity .

the webview loads TelegramWeb's page.

when i login to the telegram account it works fine.

but when i rotate screen or reopen the app it forgets all data and needs relogin to telegram account.

so i need to save some data like coockies and other necessary files.

i used setJavaScriptEnabled=true and some other setting below:

@SuppressLint("SetJavaScriptEnabled") public class SubActivity extends Activity {

private WebView wv1;

protected void onSaveInstanceState(Bundle outState) {
      wv1.saveState(outState);
   }

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sub);
    Button b1=(Button) findViewById(R.id.btnSave);

     wv1=(WebView)findViewById(R.id.webView1);
     WebSettings ws=wv1.getSettings();
     ws.setAllowContentAccess(true);
     ws.setAppCacheEnabled(true);
     ws.setSaveFormData(true);
      wv1.setWebViewClient(new Webview());
      wv1.getSettings().getCacheMode();
      wv1.getSettings().getAllowContentAccess();
      wv1.getSettings().getSaveFormData();
      wv1.getSettings().setLoadsImagesAutomatically(true);
      wv1.getSettings().setJavaScriptEnabled(true);
      wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
      wv1.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);   
      wv1.getSettings().setAllowFileAccess(true); 
      wv1.getSettings().setDomStorageEnabled(true);
      wv1.setSaveEnabled(true);
      if (savedInstanceState != null) 
         wv1.restoreState(savedInstanceState);
      else
      wv1.loadUrl("https://web.telegram.org/#/im");


      b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Toast.makeText(getBaseContext(), wv1.getUrl().toString(), Toast.LENGTH_LONG).show();
        }
    });
}

}

but still it needs re-login to account.

better to know that SubActivity Class start from MainActivity after checking a password.

------

note:

i added this code after wf1.loadUrl and it works on screen rotation.

wv1.saveState(savedInstanceState);

but still not works when close app and reopen it. so i have to re-login. i think i have to save the InstanceState some where. how can i do?

Sajjad.HS
  • 381
  • 2
  • 8
  • 19
  • i think i should serialize Bundle directly to save some where. or cast it to an object then serialize it, so when i need it can be deserialized then down-cast to Bundle. is it possible? – Sajjad.HS Feb 04 '17 at 16:02

2 Answers2

0

If you don't want to use database for saving data you can use Shared Preferences where you save key-value pairs. More in documentation.

Domen Jakofčič
  • 626
  • 1
  • 8
  • 24
0

Try the below, it might works for cache issue,

wv1.getSettings().setDomStorageEnabled(true);
wv1.getSettings().setAppCacheEnabled(true);

The below works for device rotation and configuration changes

@Override
public void onConfigurationChanged(Configuration newConfig){
    super.onConfigurationChanged(newConfig);
}

After this edit AndroidManifest.xml file like below android:configChanges="orientation|screenSize|keyboardHidden"

Riswan
  • 334
  • 3
  • 5