[英]Method does not override method from its superclass, Android Fragment with onBackPressed()
So I have a pretty simple webview application and I am trying to implement an onBackPressed() so that when they click the back button, the user goes back to the previous page.所以我有一个非常简单的 webview 应用程序,我正在尝试实现一个 onBackPressed() 以便当他们单击后退按钮时,用户会返回上一页。
I looked online and it states that I have to add this:我在网上看了一下,它指出我必须添加以下内容:
@Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
This is the activity that I am trying to add the onBackPressed() on:这是我尝试添加 onBackPressed() 的活动:
public class ForumActivity extends Fragment {
private WebView webView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_forum, container, false);
final WebView webView = (WebView) view.findViewById(R.id.forum_view);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
return false;
}
});
// mWebView settings...
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAppCacheEnabled(true);
settings.setLoadWithOverviewMode(true);
settings.setUseWideViewPort(true);
settings.setSupportZoom(true);
settings.setBuiltInZoomControls(true);
settings.setDisplayZoomControls(false);
settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
settings.setDomStorageEnabled(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
} else {
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
// Load URL
webView.loadUrl("link");
return view;
}
When I tried adding it initially, this happens .当我最初尝试添加它时,会发生这种情况。 The error it shows is "Method does not override method from its superclass".
它显示的错误是“方法不会覆盖其超类中的方法”。
What should I do?我该怎么办?
This is the activity that I am trying to add the onBackPressed() on:
这是我尝试添加 onBackPressed() 的活动:
You're linking the code for a Fragment.您正在链接片段的代码。 Not an Activity.
不是活动。 There's a huge difference.
有很大的不同。
Fragments do not have a callback to intercept back presses.片段没有回调来拦截回按。
Activities do.活动做。
You've named that class 'ForumActivity', but it is actually a Fragment.您已将该类命名为“ForumActivity”,但它实际上是一个 Fragment。
So you'll have to override the method in your Activity, and then link that up with your Fragment.因此,您必须覆盖 Activity 中的方法,然后将其与您的 Fragment 相关联。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.