我的应用程序是一个新闻应用程序。由5个或更多标签组成(这将是基于每个用户的要求的设置)。
当应用程序启动时,我动态创建5个选项卡并创建一个webview作为每个选项卡的意图,我只需要将每个选项卡的URL传递给我的代码。
这是主要活动
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);
}
}
这是我的Webview Creator活动
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=");
}
}
答案 0 :(得分:1)
您可以像这样使用Bundle
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);
答案 1 :(得分:1)
如果它对任何人有帮助,这里是@Shehabix要求的额外信息:
所以开始webview活动,如接受的答案所示
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);
以下是如何在webview活动中拦截此信息
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);
}
}