2014-01-08 24 views
0

* 當前使用導航抽屜我爲每個選項卡都有一個web視圖。我能夠加載頁面並導航,但我無法返回。關閉應用程序。下面的onbackpressed只適用於1個webview,它是webview1(後面爲此工作),但其他webview後關閉了應用程序。有沒有辦法添加多個webview onbackpressed *在Android中的多個Webview的後推導航抽屜

public class MainActivity extends ActionBarActivity 
     implements NavigationDrawerFragment.NavigationDrawerCallbacks { 

    ****@Override 
public void onBackPressed() { 
    WebView webView1; 
    webView1 = (WebView) findViewById(R.id.webView1); 
    // If there is history in the web view, go back 
    if (webView1.canGoBack()) { 
     webView1.goBack(); 
     return; 
    } 
    //Handle anything else 
    super.onBackPressed(); 
}**** 



    /** 
    * Fragment managing the behaviors, interactions and presentation of the navigation drawer. 
    */ 
    private NavigationDrawerFragment mNavigationDrawerFragment; 

    /** 
    * Used to store the last screen title. For use in {@link #restoreActionBar()}. 
    */ 
    private CharSequence mTitle; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mNavigationDrawerFragment = (NavigationDrawerFragment) 
       getSupportFragmentManager().findFragmentById(R.id.navigation_drawer); 
     mTitle = getTitle(); 

     // Set up the drawer. 
     mNavigationDrawerFragment.setUp(
       R.id.navigation_drawer, 
       (DrawerLayout) findViewById(R.id.drawer_layout)); 
    } 

    @Override 
    public void onNavigationDrawerItemSelected(int position) { 
     // update the main content by replacing fragments 
     FragmentManager fragmentManager = getSupportFragmentManager(); 
     fragmentManager.beginTransaction() 
       .replace(R.id.container, PlaceholderFragment.newInstance(position + 1)) 
       .commit(); 
    } 

    public void onSectionAttached(int number) { 
     switch (number) { 
      case 1: 
       mTitle = getString(R.string.title_section1); 
       break; 
      case 2: 
       mTitle = getString(R.string.title_section2); 
       break; 
      case 3: 
       mTitle = getString(R.string.title_section3); 
       break; 
      case 4: 
       mTitle = getString(R.string.title_section4); 
       break; 
     } 
    } 

    public void restoreActionBar() { 
     ActionBar actionBar = getSupportActionBar(); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); 
     actionBar.setDisplayShowTitleEnabled(true); 
     actionBar.setTitle(mTitle); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     if (!mNavigationDrawerFragment.isDrawerOpen()) { 
      // Only show items in the action bar relevant to this screen 
      // if the drawer is not showing. Otherwise, let the drawer 
      // decide what to show in the action bar. 
      getMenuInflater().inflate(R.menu.main, menu); 
      restoreActionBar(); 
      return true; 
     } 
     return super.onCreateOptionsMenu(menu); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 




    /** 
    * A placeholder fragment containing a simple view. 
    */ 
    public static class PlaceholderFragment extends Fragment { 
     /** 
     * The fragment argument representing the section number for this 
     * fragment. 
     */ 
     public static final String ARG_OBJECT = "object"; 

     private WebView webView; 



     /** 
     * Returns a new instance of this fragment for the given section 
     * number. 
     */ 
     public static PlaceholderFragment newInstance(int sectionNumber) { 
      PlaceholderFragment fragment = new PlaceholderFragment(); 
      Bundle args = new Bundle(); 
      args.putInt(ARG_OBJECT, sectionNumber); 
      fragment.setArguments(args); 
      return fragment; 
     } 

     public PlaceholderFragment() { 
     } 



     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 

      Bundle args = getArguments(); 
      int position = args.getInt(ARG_OBJECT); 

      int tabLayout = 1; 
      switch (position) { 
       case 1: 
        tabLayout = R.layout.fragment_main; 
        break; 
       case 2: 
        tabLayout = R.layout.fashion; 
        break; 
       case 3: 
        tabLayout = R.layout.entertainment; 

        break; 
       case 4: 
        tabLayout = R.layout.computer; 
        break; 
      } 

      View rootView = inflater.inflate(tabLayout, container, false); 

      **webView = (WebView) rootView.findViewById(R.id.webView1); 
      if (webView != null) { 
       WebSettings webSettings = webView.getSettings(); 
       webSettings.setJavaScriptEnabled(true); 
       webView.setWebViewClient(new WebViewClient()); 
       webView.loadUrl("http://headgone.net/shop/index.php/motorsports"); 


      } 
      webView = (WebView) rootView.findViewById(R.id.fashion); 
      if (webView != null) { 
       WebSettings webSettings = webView.getSettings(); 
       webSettings.setJavaScriptEnabled(true); 
       webView.setWebViewClient(new WebViewClient()); 
       webView.loadUrl("http://headgone.net/shop/index.php/headgone-clothing"); 


      } 
      webView = (WebView) rootView.findViewById(R.id.entertainment); 
      if (webView != null) { 
       WebSettings webSettings = webView.getSettings(); 
       webSettings.setJavaScriptEnabled(true); 
       webView.setWebViewClient(new WebViewClient()); 
       webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 
       webView.loadUrl("http://www.headgone.net/entertainment/index.php/homepage"); 

      } 
      webView = (WebView) rootView.findViewById(R.id.computer); 
      if (webView != null) { 
       WebSettings webSettings = webView.getSettings(); 
       webSettings.setJavaScriptEnabled(true); 
       webView.setWebViewClient(new WebViewClient()); 
       webView.loadUrl("http://headgone.net/entertainment/index.php/computer-repairs");** 

      } 


      return rootView; 

     } 



}} 
+0

這可能有助於瞭解您實際嘗試在此處做什麼。 –

+0

即時嘗試添加背靠網頁瀏覽能夠回去,而當前瀏覽關閉應用程序..我使用onbackpressed更新它,但我只適用於1 webview我有每個navdrawer選項卡的webview。 – user3084738

+0

爲什麼你不在Fragment上註冊壓抑事件? – Areks

回答

0

試試這個在你的按鈕。

Fragment fragment; 
      FragmentTransaction fragmentTransaction = getActivity().getFragmentManager().beginTransaction(); 
fragment = new YourXMLWebViewName(); 
      fragmentTransaction.replace(R.id.currentXMLId, fragment).addToBackStack(null).commit();