Android:后退按钮绕过 onKeyDown 并关闭应用程序
我有一个有 4 个不同选项卡的程序。
这些选项卡之一是 ActivityGroup,其中有一个 ListView。当我单击列表项之一时,它会切换到 WebActivity:
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(TabActivity2.this, WebActivity.class);
Bundle b = new Bundle();
b.putString("URL", URLs[(int)id]);
b.putString("prevActivity", "TabActivity2");
intent.putExtras(b);
replaceContentView("web", intent);
}
});
}
public void replaceContentView(String id, Intent newIntent) {
View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView(); this.setContentView(view);
}
所以现在我们位于 WebActivity 类中。代码如下:
public class WebActivity extends ActivityGroup {
WebView mWebView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web);
Bundle b = getIntent().getExtras();
String URL = b.getString("URL");
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.loadUrl(URL);
mWebView.setWebViewClient(new FirstTabWebViewClient());
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
Bundle b = getIntent().getExtras();
String retActivity = b.getString("prevActivity");
if (retActivity == "TabActivity2") {
Intent intent = new Intent(WebActivity.this, TabActivity2.class);
replaceContentView("list_webpages", intent);
return true;
}
return super.onKeyDown(keyCode, event);
}
public void replaceContentView(String id, Intent newIntent) {
View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView(); this.setContentView(view);
}
奇怪的是:如果我在进入 WebActivity 类后立即单击后退按钮,它将关闭应用程序。
但是,如果我首先单击 WebView 内的任何链接,然后按回两次,那么它会返回到 ListView 的原始活动。
通过在 Eclipse 中使用断点,我发现如果我在进入 WebActivity 视图后立即按“后退”按钮,程序甚至不会访问 onKeyDown。但是,如果我单击 WebView 中的任何链接,然后按后退按钮,它将通过我的 onKeyDown 方法。
这是怎么回事?
I have a program with 4 different tabs.
One of these tabs is an ActivityGroup which has a ListView in it. When I click on one of the list items, it switches to WebActivity:
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(TabActivity2.this, WebActivity.class);
Bundle b = new Bundle();
b.putString("URL", URLs[(int)id]);
b.putString("prevActivity", "TabActivity2");
intent.putExtras(b);
replaceContentView("web", intent);
}
});
}
public void replaceContentView(String id, Intent newIntent) {
View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView(); this.setContentView(view);
}
So now we're in the WebActivity class. Here's the code:
public class WebActivity extends ActivityGroup {
WebView mWebView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web);
Bundle b = getIntent().getExtras();
String URL = b.getString("URL");
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.loadUrl(URL);
mWebView.setWebViewClient(new FirstTabWebViewClient());
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
Bundle b = getIntent().getExtras();
String retActivity = b.getString("prevActivity");
if (retActivity == "TabActivity2") {
Intent intent = new Intent(WebActivity.this, TabActivity2.class);
replaceContentView("list_webpages", intent);
return true;
}
return super.onKeyDown(keyCode, event);
}
public void replaceContentView(String id, Intent newIntent) {
View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView(); this.setContentView(view);
}
Here's the strange thing: If I click on the back button right after I enter the WebActivity class, it closes the application.
But if I first click on any link inside the WebView and then press back twice, then it returns me to the original activity with the ListView.
From using break points in Eclipse I discovered that the program does not even visit onKeyDown if I press the Back button right after I enter the WebActivity view. However, if I click on any link within the WebView and then press the back button, then it goes through my onKeyDown method.
What is going on here??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 WebView 没有焦点,这会导致在其之上触发后退按钮(应用程序堆栈)。如果您单击某个链接,它将返回焦点。 WebView 有一些关于焦点的奇怪问题,请查看这篇文章: Android WebView focus Problem
您还会发现一些建议的解决方法来获得焦点,您需要在正确的位置调用 WebView 上的 requestFocus() 。
Your WebView doesn't have the focus, which causes the back button to be triggered on top of that (the application stack). If you click on a link it get's back focus. The WebView has some strange issues concerning focus, have look at this post: Android WebView focus problem
There you will also find some suggested workarounds to get the focus, you need to call requestFocus() on the WebView at the right place.