我的应用程序有两个按钮,每个按钮打开不同的网页。每个按钮都引用一个 onClick 事件和仅在 URL 中不同的 Intent。主要和 webview 活动如下。但是,第二个按钮需要单击两次才能返回主视图。我怎样才能解决这个问题?
public class MainActivity extends Activity {
private Button button;
private Button button1;
public void onCreate(Bundle savedInstanceState) {
final Context context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.buttonUrl);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, WebViewActivity.class);
intent.putExtra("google.com", "http://www.google.com");
startActivity(intent);
}
});
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, WebViewActivity.class);
intent.putExtra("yahoo.com", "http://www.yahoo.com");
startActivity(intent);
}
});
}
}
WebviewActivity
public class WebViewActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
Bundle extras = getIntent().getExtras();
if (extras != null){
String googleUrl = extras.getString("google.com");
String yahooUrl = extras.getString("yahoo.com");
if (googleUrl != null)
webView.loadUrl("http://www.google.com");
else if (yahooUrl != null)
webView.loadUrl("http://www.yahoo.com");
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/buttonUrl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to http://www.google.com" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to http://www.yahoo.com" />
</LinearLayout>
webview.xml
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>