离开WebView
后,我一直试图找出一种在WebView fragment
内返回的方法。困境如下:
WebView fragment
page1.html
page1.html?navigate=page2.html
。Fragment
中的某个事件导航到其他WebView fragment
,说我点击了一个带来"NotAwebviewfragment.java"
的符号notawebviewfragment
点击然后它转到我离开的WebView page
。但是,当我再次点击它时,我会转到之前在Fragment
之前打开的WebView
。WebViewFragment
我可以导航回来
根据需要。但是,一旦我离开它,它就可以访问
WebView
只有一次。到目前为止,这是代码: 在webview中:
public boolean webViewSteppedBack() {
if (webview != null && webview.canGoBack()) {
webview.goBack();
return true;
}
return false;
}
public boolean backPressed(final MainActivity mainActivity) {
if (webViewSteppedBack()) {
if(!EikonUtils.isTablet(getActivity())) {
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
return true;
}
return false;
}
MainActivity
(包含后面的片段导航代码):
@Override
public void onBackPressed() {
final FragmentManager manager = getSupportFragmentManager();
Fragment topFragment = FragmentStackManager.getInstance().getTopFragment();
if (backPressListener != null) {
boolean b = false;
//Making sure we trigger the backPressed event if the listener is the top fragment
String bplTag = ((Fragment) backPressListener).getTag();
String topFragemtnTag = "";
if (topFragment != null) {
topFragemtnTag = topFragment.getTag();
if (bplTag != null && topFragemtnTag != null && bplTag.equals(topFragemtnTag)) {
b = backPressListener.backPressed(this);
}
}
if (b) { return; }
if (!NotAWebViewFragment.TAG_NOT.equals(bplTag)) {
backPressListener = null;
}
}
}
答案 0 :(得分:0)
您需要在定义webView的片段中覆盖此方法。
webView.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
}
}
}
return false;
}
});
答案 1 :(得分:0)
您可以将所有片段添加到back stack。在activity的onBackPressed方法中,您可以获取当前活动的片段。您可以检查活动片段是否是Awebviewfragment的实例,以及webview是否可以向后导航,否则使用片段管理器的popBackStack()方法从堆栈中弹出当前活动片段。 以下是代码段: 1.添加片段
private void addNotAwebviewfragment(){ supportFragmentManager.beginTransaction(); FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction(); notAwebviewfragment = NotAwebviewfragment.newInstance(" test"," test");
fragmentTransaction.add(R.id.contents, notAwebviewfragment, NotAwebviewfragment.TAG_NOT).addToBackStack(NotAwebviewfragment.TAG_NOT);
fragmentTransaction.commit();
}
private void addFragWebview(){
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
webViewFragment = Awebviewfragment.newInstance("test", "test");
webViewFragment.setFragmentListener(weblistener);
backPressListener = webViewFragment;
fragmentTransaction.add(R.id.contents, webViewFragment, Awebviewfragment.TAG).addToBackStack(Awebviewfragment.TAG);
fragmentTransaction.commit();
}
在主要活动的onBackPRessed()中: @覆盖 public void onBackPressed(){
Fragment fragment = getActiveFragment();
if(fragment instanceof Awebviewfragment){
System.out.println("web view fragment");
if(backPressListener != null && backPressListener.backPressed(this)){
System.out.println("web view fragment will return");
return;
}
}
int count = supportFragmentManager.getBackStackEntryCount();
if (count > 0 ){
supportFragmentManager.popBackStackImmediate();
}else {
super.onBackPressed();
}
}
您可以查看github中提交的示例。