1

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=");
    }
} 
Shehabic
  • 6,787
  • 9
  • 52
  • 93

2 Answers2

1

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);
Gaurav Agarwal
  • 18,754
  • 29
  • 105
  • 166
  • ok Now I have set this in the NewsMobile activity.. Now when I tried to read it in the WebviewActivity it gives errors browse.loadUrl( savedInstanceState.getString("url") ); could you please apply your solution on the code above ? I'm a beginner JAVA developer, I'm originally PHP/Python Developer – Shehabic Apr 18 '12 at 23:55
  • @Shehabix According to StackOverflow policy,that is a separate question, you ask another question, leave a comment here for me, I will check the question and leave a answer. – Gaurav Agarwal Apr 19 '12 at 11:26
  • :: Pass = Send & Receive , so that's not another question . . though I found it somewhere else . – Shehabic Apr 20 '12 at 18:16
1

In case it helps anyone, here is the extra information that @Shehabix was asking for:

So start the webview activity as shown in the accepted answer

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);

Here is how to intercept this information in the webview activity

public class MyWebView extends Activity {

    private WebView webView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.asset_web_view);
        //get url stored in intent
        String url = super.getIntent().getExtras().getString("urlString");
        loadUrlInWebView(url);
    }

    private void loadUrlInWebView(String url){
        webView = (WebView) findViewById(R.id.mywebview);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl(url);
    }
}
computingfreak
  • 4,939
  • 1
  • 34
  • 51
mjj1409
  • 3,075
  • 6
  • 28
  • 28