简体   繁体   English

从webview片段导航到不同的片段后,如何在webview中返回?

[英]How do I go back within webview after navigation to a different fragment from the webview fragment?

I have been trying to figure out a way to go back within WebView after leaving the WebView fragment . 在离开WebView fragment后,我一直想找到一种在WebView返回的方法。 The dilemma is as follows: 困境如下:

  1. I load the WebView fragment page1.html 我加载WebView fragment page1.html
  2. Then I navigate within it such that page1.html?navigate=page2.html . 然后我在其中导航,例如page1.html?navigate=page2.html
  3. Then I navigate to a different Fragment on click of some event in the WebView fragment , say I click a symbol that brings in "NotAwebviewfragment.java" 然后,我导航到不同的Fragment上的某些事件的点击WebView fragment ,说我点击,在带来了一个符号"NotAwebviewfragment.java"
  4. Then I click back from notawebviewfragment then it takes me to the WebView page I left from. 然后我从notawebviewfragment单击回来然后它转到我离开的WebView page However when I click back again it take me to the previous Fragment that was open before WebView . 但是,当我再次单击它时,它会转到之前在WebView之前打开的Fragment
  5. Now If I stay within the WebViewFragment I can navigate back and forth as desired. 现在如果我留在WebViewFragment我可以根据需要来回导航。 However, once I leave it, it gives me access to the WebView only once. 但是,一旦我离开它,它只允许我访问WebView一次。

Here's the code so far: Within the 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 (which contains the fragment navigation code for back): 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;
 } 

 }
}

You need to override this method in your fragment in which you are defining the webView. 您需要在定义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;
    }
});

You can add all the fragment to the back stack . 您可以将所有片段添加到后台堆栈 In activity's onBackPressed method, you can get the current active fragment. 在activity的onBackPressed方法中,您可以获取当前活动的片段。 You can check if the active fragment is the instance of Awebviewfragment and if the webview can navigate back otherwise pop the current active fragment from the stack using popBackStack () method of fragment manager. 您可以检查活动片段是否是Awebviewfragment的实例,如果webview可以导航回来,否则使用片段管理器的popBackStack ()方法从堆栈中弹出当前活动片段。 Here are the code snippet: 1. to add fragment 以下是代码段:1。添加片段

private void addNotAwebviewfragment() { supportFragmentManager.beginTransaction(); private void addNotAwebviewfragment(){supportFragmentManager.beginTransaction(); FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction(); FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction(); notAwebviewfragment = NotAwebviewfragment.newInstance("test", "test"); 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();
}
  1. In onBackPRessed() in main activity: @Override public void onBackPressed() { 在主活动中的onBackPRessed()中:@Override 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(); } 

    } }

  2. get active fragment: public Fragment getActiveFragment() { if (supportFragmentManager.getBackStackEntryCount() == 0) { return null; get active fragment:public Fragment getActiveFragment(){if(supportFragmentManager.getBackStackEntryCount()== 0){return null; } String tag = supportFragmentManager.getBackStackEntryAt(supportFragmentManager.getBackStackEntryCount() - 1).getName(); } String tag = supportFragmentManager.getBackStackEntryAt(supportFragmentManager.getBackStackEntryCount() - 1).getName(); return supportFragmentManager.findFragmentByTag(tag); return supportFragmentManager.findFragmentByTag(tag); } }

you can check with the sample committed in the github . 你可以检查github中提交的样本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM