我用webview顯示網站裏面的片段,應用程序退出
加載哦網站上,當我點擊後備按鈕,我的應用程序退出。
這裏是我的代碼:
webview.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN)
{
WebView webView = (WebView) v;
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webView.canGoBack())
{
webView.goBack();
return true;
}
else
{
Fragment fragment = new HomeFragment();
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();
} }
break;
}
} return false;
}
});
你有沒有其他視圖在堆棧? –