带有 Webview 的片段利用硬件后退按钮转到上一个网页
Posted
技术标签:
【中文标题】带有 Webview 的片段利用硬件后退按钮转到上一个网页【英文标题】:Fragment with Webview utilize harware back button to go to previous webpage 【发布时间】:2015-07-09 22:33:51 【问题描述】:这似乎在堆栈溢出时被问了很多,但随着 SDK 的更改似乎不再可以实现答案(我可能错了!)。
我一直在尝试允许 MainActivity 中的片段使用硬件按钮返回片段中 web 视图中的上一页。
我尝试过使用 return super.onKeyDown(keyCode, event); 作为函数的一部分,但 onKeyDown 在我的项目中无效。
这是我的包含 web 视图的 BlogFragment:
public class BlogFragment extends Fragment
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
WebView wv;
private OnFragmentInteractionListener mListener;
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment blog.
*/
// TODO: Rename and change types and number of parameters
public static BlogFragment newInstance(String param1, String param2)
BlogFragment fragment = new BlogFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
public static boolean canGoBack()
return wv.canGoBack();
public static void goBack()
wv.goBack();
public BlogFragment()
// Required empty public constructor
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
if (getArguments() != null)
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
View view=inflater.inflate(R.layout.fragment_blog, null);
wv = (WebView) view.findViewById(R.id.webview);
WebSettings settings = wv.getSettings();
wv.setWebChromeClient(new WebChromeClient()
);
final String mimeType = "text/html";
final String encoding = "UTF-8";
String html = getHTML();
settings.setjavascriptEnabled(true);
wv.loadDataWithBaseURL("http://www.bbc.co.uk", html, mimeType, encoding, "");
return view;
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri)
if (mListener != null)
mListener.onFragmentInteraction(uri);
@Override
public void onAttach(Activity activity)
super.onAttach(activity);
try
mListener = (OnFragmentInteractionListener) activity;
catch (ClassCastException e)
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
@Override
public void onDetach()
super.onDetach();
mListener = null;
public String getHTML()
String html = "<iframe width=\"100%\" height=\"100%\" src=\"http://blog.mrgyro.co.uk\" frameborder=\"0\" allowfullscreen></iframe>";
return html;
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener
// TODO: Update argument type and name
public void onFragmentInteraction(Uri uri);
活动:
public class MainActivity extends ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks, BlogFragment.OnFragmentInteractionListener, HomeFragment.OnFragmentInteractionListener
/**
* 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 onBackPressed()
if(BlogFragment.canGoBack())
BlogFragment.goBack();
else
super.onBackPressed();
@Override
public void onNavigationDrawerItemSelected(int position)
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
switch (position)
case 0:
HomeFragment homeFragment = new HomeFragment();
transaction.replace(R.id.container, homeFragment);
break;
case 1:
BlogFragment blogFragment = new BlogFragment();
transaction.replace(R.id.container, blogFragment);
break;
transaction.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();
//noinspection SimplifiableIfStatement
//if (id == R.id.action_settings)
// return true;
//
return super.onOptionsItemSelected(item);
@Override
public void onFragmentInteraction(Uri uri)
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* 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_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
public PlaceholderFragment()
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View rootView = null;
rootView = inflater.inflate(R.layout.fragment_home, container, false);
switch(getArguments().getInt(ARG_SECTION_NUMBER))
case 1:
rootView = inflater.inflate(R.layout.fragment_home, container, false);
break;
case 2:
rootView = inflater.inflate(R.layout.fragment_blog, container, false);
break;
case 3:
// rootView = inflater.inflate(R.layout.fragment_test, container, false);
break;
case 4:
//rootView = inflater.inflate(R.layout.fragment_info, container, false);
break;
default:
Log.e("TAG", "Unrecognized section: " + getArguments().getInt(ARG_SECTION_NUMBER));
return rootView;
@Override
public void onAttach(Activity activity)
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
提前致谢。
编辑:我已经用我最近的尝试更新了博客片段,但是当尝试静态 wv.canGoBack() 和 wv.goBack() 时都返回错误“非静态字段 'wv' 不能从静态上下文
【问题讨论】:
【参考方案1】:尝试在您的Activity
中覆盖onBackPressed() 并使其戳WebView
。
顺便说一句 - 您也可以发布包含 onKeyDown
方法的 Activity
。
编辑:首先实例化片段:
BlogFragment blogFragment = new BlogFragment();
blogFragment.canGoBack();
然后从您的方法中删除static
。 :)
OPs 实施(感谢 Klotor) 在 MainActivity 实现的头部:
BlogFragment blogFragment = new BlogFragment();
然后执行:
public void onBackPressed()
blogFragment.canGoBack();
if(blogFragment.canGoBack())
blogFragment.goBack();
else
super.onBackPressed();
片段在 onBackPressed 之外实例化,以防止在使用方法在片段之间导航时崩溃。 在这种情况下,您需要实例化片段的新实例,而不是一个全新的片段。
【讨论】:
你好@Klotor,我已经尝试过这个,我遇到了我作为编辑添加到我原来的帖子中的错误。我也包括了这个活动。 @Lister 好吧,要么删除static
,要么在你的 canGoBack
和 goBack
方法中添加一个参数,我会编辑我的答案
我已经删除了静态,但是现在当调用 BlogFragment.CanGoBack() 和 goBack 时,我得到一个“非静态方法 .goBack() 不能从静态上下文中引用。工作室建议的修复是读取静态,但这又会导致原来的问题。
在你调用它之前把我添加到答案中的代码放在你调用方法的行上,将大写 B
更改为一个小写 ;)
放在Activity中的onBackPressed() :)【参考方案2】:
** 在 OnCreateView 内的 Fragment 类中放入以下代码**
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.your_layout, container, false);
mWebView = (WebView) view.findViewById(R.id.webView);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new MyWebViewClient());
mWebView.loadUrl(expertsUrl);
return view;
private class MyWebViewClient extends WebViewClient
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
view.loadUrl(url);
return true;
**在 MainActivity 类中放下面的代码 **
@Override
public void onBackPressed()
if (WebViewFragment.mWebView!=null)
if (WebViewFragment.mWebView.canGoBack())
WebViewFragment.mWebView.goBack();
else
super.onBackPressed();
用你的 Fragment 替换 WebViewFragment -> mWebView -> 与您的网络视图
希望对你有帮助!!!!
【讨论】:
以上是关于带有 Webview 的片段利用硬件后退按钮转到上一个网页的主要内容,如果未能解决你的问题,请参考以下文章
如果在 WebView 片段中按下后退按钮,如何返回上一页?