0

看起来我已经非常接近我的 WebView 片段返回问题的解决方案了。这是我的活动代码:

package com.fragmentwebtest;

import java.util.ArrayList;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
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.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;

public class TestSwipeABActivity extends FragmentActivity {

FragmentTransaction transaction;
static ViewPager mViewPager;
/** Called when the activity is first created. */
Fragment tabOneFragment = new TabOne();
Fragment tabTwoFragment = new TabTwo();
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    PagerAdapter mPagerAdapter = new PagerAdapter(getSupportFragmentManager());
    mPagerAdapter.addFragment(tabOneFragment);
    mPagerAdapter.addFragment(tabTwoFragment);

    //transaction = getSupportFragmentManager().beginTransaction();

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mPagerAdapter);
    mViewPager.setOffscreenPageLimit(2);
    mViewPager.setCurrentItem(0);

    mViewPager.setOnPageChangeListener(
            new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    // When swiping between pages, select the
                    // corresponding tab.
                    getActionBar().setSelectedNavigationItem(position);
                }
            });

    ActionBar ab = getActionBar();
    ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    Tab tab1 = ab.newTab().setText("Tab One")
            .setTabListener(new TabListener<TabOne>(
                    this, "tabone", TabOne.class));

    Tab tab2 = ab.newTab().setText("Tab Two")
            .setTabListener(new TabListener<TabTwo>(
                    this, "tabtwo", TabTwo.class));

    ab.addTab(tab1);
    ab.addTab(tab2);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

@Override
public void onBackPressed(){
    if ( (TabOne) tabOneFragment != null )
        ( (TabOne) tabOneFragment).onBackPressed();
    else
        super.onBackPressed();
}

public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
    private Fragment mFragment;
    private final Activity mActivity;
    private final String mTag;
    private final Class<T> mClass;

    /** Constructor used each time a new tab is created.
      * @param activity  The host Activity, used to instantiate the fragment
      * @param tag  The identifier tag for the fragment
      * @param clz  The fragment's Class, used to instantiate the fragment
      */
    public TabListener(Activity activity, String tag, Class<T> clz) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
    }

    /* The following are each of the ActionBar.TabListener callbacks */

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // Check if the fragment is already initialized
        if (mFragment == null) {
            // If not, instantiate and add it to the activity
            mFragment = Fragment.instantiate(mActivity, mClass.getName());
            ft.add(android.R.id.content, mFragment, mTag);
        } else {
            // If it exists, simply attach it in order to show it
            ft.attach(mFragment);
        }
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        if (mFragment != null) {
            // Detach the fragment, because another one is being attached
            ft.detach(mFragment);
        }
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // User selected the already selected tab. Usually do nothing.
    }

    public void onTabReselected(Tab arg0,
            android.app.FragmentTransaction arg1)
    {
        // TODO Auto-generated method stub

    }

    public void onTabSelected(Tab arg0, android.app.FragmentTransaction arg1)
    {
        // TODO Auto-generated method stub
        mViewPager.setCurrentItem(arg0.getPosition());
    }

    public void onTabUnselected(Tab arg0,
            android.app.FragmentTransaction arg1)
    {
        // TODO Auto-generated method stub

    }
}

public class PagerAdapter extends FragmentPagerAdapter {

    private final ArrayList<Fragment> mFragments = new ArrayList<Fragment>();

    public PagerAdapter(FragmentManager manager) {
        super(manager);
    }

    public void addFragment(Fragment fragment) {
        mFragments.add(fragment);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mFragments.size();
    }

    @Override
    public Fragment getItem(int position) {
        return mFragments.get(position);
    }
}

}

好,现在是 TabOne 片段:

package com.fragmentwebtest;

import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class TabOne extends Fragment
{
 private WebView myWebView;
 @Override
    public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    }

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.tabone, container, false);
    myWebView = (WebView) view.findViewById(R.id.webview);
    myWebView.getSettings().setPluginsEnabled(true);
    myWebView.loadUrl("http://m.stirileprotv.ro");
    myWebView.setWebViewClient(new WebViewClient() {
    @Override public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (Uri.parse(url).getHost().equals("m.stirileprotv.ro")) {
                myWebView.loadUrl(url);
                return true;
            }
            else
            {
                return false;
            }
         }
    });
    return view;
}
public void onBackPressed() {
    if (getActivity() == null)
        return;
    if (myWebView != null && myWebView.canGoBack()) {
        myWebView.goBack();
    } else
        getActivity().finish();
}
}

我不知道出了什么问题,为什么当我按下后退按钮时,WebView 不会返回。请有人帮助我!谢谢!

4

1 回答 1

0

The onKeyDown on your activity is suppressing the back key completely since you are always only returning true if the event is from the back key. So onBackPressed never gets called.

Try this instead:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        onBackPressed();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
于 2012-09-09T13:00:11.043 回答