Search code examples
androidandroid-intentandroid-webviewandroid-tabactivity

How do I pass dynamic URL to each Webview Activity in a multi-tab with 1 web view in each tab - Android App


My Application is a news App. composed of 5 Tabs or More (this will be a setting based on each user's requirement).

When the application Start I create the 5 Tabs dynamically and create a webview as an intent for each Tab, I just need to pass the URL for each tab to the intent here's my Code.

This is the main activity

package news.mobile;

import android.app.Activity;
import android.os.Bundle;
import android.app.TabActivity;
import android.widget.TabWidget;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.content.Intent;

public class NewsMobile extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TabHost tabHost = getTabHost();
        // Here I create the tabs dynamically.
        for(int i=0;i<5;i++){
        tabHost.addTab(
                tabHost.newTabSpec("tab"+i)
                .setIndicator("Politics")
                    // I need to pass an argument to the WebviewActivity to open a
                    // specific URL assume it is "http://mysite.com/?category="+i
                .setContent( new Intent(this, WebviewActivity.class)));
        }
        tabHost.setCurrentTab(0);
    }
}

This is my Webview Creator Activity

package news.mobile;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class WebviewActivity extends Activity {
    WebView browse;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        browse=new WebView(this);
        setContentView(browse);
                // I need the following line to read an argument and add it to the url
        browse.loadUrl("http://mysite.com/?category=");
    }
} 

Solution

  • You can use Bundle like this

    Bundle bundle = new Bundle();
    String url = "http://www.google.com";
    bundle.putString("urlString", url);
    Intent intent = new Intent(ThisActivity.this, NewActivity.class);
    intent.putExtras(bundle);
    startActivity(intent);