4

I'm fairly new to Android development, and I have a question about how WebViews handle data (in Java).

I'm assuming this would fall under the 'cookie' category. But what I have is two different webViews on different tabs of my application. I would like for one webView (call it webView1) to be logged into one account of a website, while the other (webView2) to be logged into another account of the same website. For example, I would want to log into two separate Gmail accounts within the two webViews at the same time.

The problem I am having is that once I log into an account on webView1, webView2 follows suit and logs me into that account. The same problem happens when I log into webView2, as webView1 logs into that account also naturally.

Is there any way to get around this? I want my two webViews to act completely independently from one another is what it comes down to.

Thanks!

jszumski
  • 7,430
  • 11
  • 40
  • 53

1 Answers1

0

I can definitely answer your question. I am mainly an iOS developer and I am creating an Android app with the exact opposite effect of webView cookies not sharing. I created my webviews in tabs to simulate the behavior I use in iOS, but my webViews are acting completely independant of one another. I want them to share the same login info, but as it stands I must login to each tab individually because they aren't playing nice.

Either way, my problem can definitely help you and I would hope that you share your info with me to create the cookie sharing effect I am searching for...

Here is my main.xml layout:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <WebView
            android:id="@+id/web_engine"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_gravity="bottom"
            android:layout_marginTop="-45dp" />
        <WebView
            android:id="@+id/messages"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_gravity="bottom"
            android:layout_marginTop="-45dp" />
        <WebView
            android:id="@+id/myprofile"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_gravity="bottom"
            android:layout_marginTop="-45dp" />
        <WebView
            android:id="@+id/rncorner"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_gravity="bottom"
            android:layout_marginTop="-45dp" />
    </FrameLayout>
</LinearLayout>
</TabHost>

Here is my Activity:

package com.example.tabs;


import android.app.TabActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TabHost;

public class TabsActivity extends TabActivity {
WebView webView;

final String DEFAULT_URL = "http://example.com";


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);

    TabHost mTabHost = getTabHost();

    mTabHost.addTab(mTabHost.newTabSpec("Home").setIndicator("Home",getResources().getDrawable(R.drawable.home)).setContent(R.id.web_engine));
    mTabHost.addTab(mTabHost.newTabSpec("Messages").setIndicator("Messages",getResources().getDrawable(R.drawable.messages)).setContent(R.id.messages));
    mTabHost.addTab(mTabHost.newTabSpec("My Profile").setIndicator("My Profile", getResources().getDrawable(R.drawable.myprofile)).setContent(R.id.myprofile));
    mTabHost.addTab(mTabHost.newTabSpec("Map").setIndicator("Map", getResources().getDrawable(R.drawable.rncorner)).setContent(R.id.rncorner));

    mTabHost.setCurrentTab(0);

    //home
    webView = (WebView)findViewById(R.id.web_engine);

    webView.setWebViewClient(new MyWebViewClient());

    webView.loadUrl(DEFAULT_URL);

    //messages
    webView = (WebView)findViewById(R.id.messages);

    webView.setWebViewClient(new MyWebViewClient());

    webView.loadUrl("http://example.com/index.php2");

    //my profile
    webView = (WebView)findViewById(R.id.myprofile);

    webView.setWebViewClient(new MyWebViewClient());

    webView.loadUrl("http://example.com/index.php3");


    //rncorner
    webView = (WebView)findViewById(R.id.rncorner);

    webView.setWebViewClient(new MyWebViewClient());

    webView.loadUrl("http://example.com/index.php4");

}



public class MyWebViewClient extends WebViewClient {



    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        // TODO Auto-generated method stub

        view.loadUrl(url);

        return true;

    } 


}
}
INSITE MOBILE
  • 133
  • 1
  • 10