WebView 的问题...返回到之前的活动
我有 3 个活动 - A、B 和 C。我有一个关于活动 A 的列表,如果单击它,它会转到活动 B。在活动 B 上,我有一个按钮可以打开活动 C(Web 查看器),其中包含以下内容代码:
buyButton = (Button) findViewById(R.id.buyButton);
buyButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
System.out.println("!!! buy !!!");
/*Uri uri = Uri.parse("http://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);*/
Intent startActivity = new Intent(getApplicationContext(), WebViewer.class);
startActivity.putExtra("link","http://www.google.com");
startActivityForResult(startActivity, 15);
}
});
现在,当我按下 Web 查看器活动 C 上的后退按钮时,活动 B 也会关闭。我不知道为什么会发生这种情况。有什么提示吗?谢谢。
网络查看器代码:
public class WebViewer extends Activity {
ImageButton backButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.streams);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title3);
String url = "";
Bundle extras = getIntent().getExtras();
if(extras !=null) {
url = extras.getString("link");
}
if (!url.startsWith("http://") && !url.startsWith("https://"))
url = "http://" + url;
final TextView title=(TextView) findViewById(R.id.title_text_view_success3);
//title.setText("Saved Streams");
backButton = (ImageButton) findViewById(R.id.back_button3);
backButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
System.out.println("!!! BACK !!!");
//finishActivity(0);
onBackPressed();
}
});
setContentView(R.layout.viewer);
final WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
title.setText("Loading...");
WebViewer.this.setProgress(progress * 100);
if(progress == 100)
title.setText(webView.getTitle());
}
});
webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
webView.loadUrl(url);
}
}
I have 3 activities- A, B and C. I have a list on activity A and if I click on it, it goes to activity B. On activity B, I have a button which opens activity C (web viewer) with the following code :
buyButton = (Button) findViewById(R.id.buyButton);
buyButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
System.out.println("!!! buy !!!");
/*Uri uri = Uri.parse("http://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);*/
Intent startActivity = new Intent(getApplicationContext(), WebViewer.class);
startActivity.putExtra("link","http://www.google.com");
startActivityForResult(startActivity, 15);
}
});
Now, when I press the back button on web viewer activity C, activity B also closes. I am not sure why that is happening. Any hints ? Thanks.
webviewer code :
public class WebViewer extends Activity {
ImageButton backButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.streams);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title3);
String url = "";
Bundle extras = getIntent().getExtras();
if(extras !=null) {
url = extras.getString("link");
}
if (!url.startsWith("http://") && !url.startsWith("https://"))
url = "http://" + url;
final TextView title=(TextView) findViewById(R.id.title_text_view_success3);
//title.setText("Saved Streams");
backButton = (ImageButton) findViewById(R.id.back_button3);
backButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
System.out.println("!!! BACK !!!");
//finishActivity(0);
onBackPressed();
}
});
setContentView(R.layout.viewer);
final WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
title.setText("Loading...");
WebViewer.this.setProgress(progress * 100);
if(progress == 100)
title.setText(webView.getTitle());
}
});
webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
webView.loadUrl(url);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更改您的代码,如下所示,
change your code like below,
首先,我认为在创建
Intent
时不应该使用 getApplicationContext。您应该只使用对当前所在活动的引用。这样 Android 将结果返回给该活动。例子:
Well for one I don't think you should use getApplicationContext when creating an
Intent
. You should just use a reference to the activity you are currently in. That way Android returns the result to that activity.Example:
我不确定我是否理解你的问题。但如果您想返回,请使用下面的代码。我在网络查看器中使用此代码。
I am not sure if I understand your question. but if you want to go back then use the code below. I use this code in webviewer.