在webview片段中实现后退按钮

时间:2013-09-17 05:47:53

标签: android webview android-fragments back

我想在我的应用中实现后退按钮。我正在使用每个显示不同webview的片段。现在,如果我按下后退按钮,它会关闭应用程序,无论我在哪里。我希望它能像浏览器一样回归。我有MainActivity.javafragment1.java等,直至fragment5.java

这是我的MainActivity.java

package com.----;

import java.util.Locale;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings;

public class MainActivity extends FragmentActivity {

    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager            mViewPager;




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

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);


    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }


        @Override
        public Fragment getItem(int position) {

            Fragment fragment;
            switch (position) {
                case 0:
                    fragment = new fragment1();
                    break;
                case 1:
                    fragment = new fragment2();
                    break;
                case 2:
                    fragment = new fragment3();
                    break;
                case 3:
                    fragment = new fragment4();
                    break;
                case 4:
                    fragment = new fragment5();
                    break;
                default:
                    fragment  = null;
                    break;
            }
            return fragment;

        }

        @Override
        public int getCount() {
            return 5;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
                case 0:
                    return getString(R.string.tab1).toUpperCase(l);
                case 1:
                    return getString(R.string.tab2).toUpperCase(l);
                case 2:
                    return getString(R.string.tab3).toUpperCase(l);
                case 3:
                    return getString(R.string.tab4).toUpperCase(l);
                case 4:
                    return getString(R.string.tab5).toUpperCase(l);
            }
            return null;
        }
    }
}

这是我的fragment1.java:

package com.----;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebSettings;

public class fragment1 extends Fragment {


    WebView myWebView;



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


        View root = inflater.inflate(R.layout.fragment1, container, false);

        myWebView = (WebView) root.findViewById(R.id.webview1);
        myWebView.setWebViewClient(new WebViewClient());
        myWebView.loadUrl("url");
        return root;

    }
}

如果我尝试使用此solutions并在测试时关闭力量并按下后退按钮。

Here是它的logcat。

4 个答案:

答案 0 :(得分:1)

在您的活动覆盖背景:

@Override
    public void onBackPressed() {
        switch (mViewPager.getCurrentItem()) {
        case 0:
            if (!webViewGoBack(0)) {
                    //do something if webview cannot go back
            }
            break;
        case 1:

            break;
        default:

        }
    }

public boolean webViewGoBack(int num) {
        SectionsPagerAdapter adapter = ((SectionsPagerAdapter)mViewPager.getAdapter());
        Fragment f = (Fragment )adapter.getFragment(num);
        if (f!= null) {
            return f.webViewGoBack();
        }
        return false;
    }

w.webViewGoBack()片段中的方法:

public boolean WebViewGoBack() {
if(webView.canGoBack()){
   webView.goBack();
   return true;
}
return false; //webview cannot go back, so use the method of the BackButton
}

答案 1 :(得分:0)

只需覆盖您的活动的onBackPressed()方法

答案 2 :(得分:0)

FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager
                        .beginTransaction();
                First first_act = new First();

                fragmentTransaction.replace(R.id.fragment_container,
                        first_act);
                fragmentTransaction.addToBackStack("first_act");
                fragmentTransaction.commit();

在mainactivity中添加这些代码, 然后在片段

中使用此onbackpressed
@Override
  public void onBackPressed() {

  }

答案 3 :(得分:-1)

您可以首先覆盖MainActivity中的后退按钮

@Override
public void onBackPressed() {
    //super.onBackPressed()
    //handle the press here to switch fragments
}