我真的看过每个问题,让后面的按钮与片段中的WebView一起工作,似乎无法使它在我的情况下工作。
我目前正在努力让blackbelt's回答工作,但是一旦按下后退按钮,我的应用就会关闭。
任何帮助都会很棒,谢谢。
public class MainActivity extends BaseActivity {
private Fragment FragmentArchietecture = new FragmentShakira();
WebView webview;
private Fragment mContent;
public MainActivity() {
super(R.string.app_name);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null)
mContent = getSupportFragmentManager().getFragment(
savedInstanceState, "mContent");
if (mContent == null)
mContent = new FragmentShakira();
setContentView(R.layout.content_frame);
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, FragmentArchietecture, "webby")
.commit();
setBehindContentView(R.layout.menu_frame);
getSupportFragmentManager().beginTransaction()
.replace(R.id.menu_frame, new RandomList()).commit();
ActionBar bar = getSupportActionBar();
bar.setTitle(R.string.app_name);
getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
setSlidingActionBarEnabled(true);
}
@Override
public void onBackPressed() {
Fragment webview = getSupportFragmentManager().findFragmentByTag(
"webby");
if (webview instanceof FragmentShakira) {
boolean goback = ((FragmentShakira) webview).canGoBack();
if (!goback)
super.onBackPressed();
}
}
}
片段:
public class FragmentShakira extends SherlockFragment {
static WebView webview;
String TAG = "webby";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater
.inflate(R.layout.fragmentwebview, container, false);
WebView webview = (WebView) view.findViewById(R.id.webView1);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient());
webview.loadUrl("https://www.google.co.uk/");
return view;
}
public boolean canGoBack() {
return webview != null && webview.canGoBack();
}
}
答案 0 :(得分:1)
它应该有用。如果可以
,请不要忘记将消息传递给片段goback即
if (!goback)
super.onBackPressed();
else
((FragmentShakira) view).goBack();
答案 1 :(得分:0)
使用Nawrez here发布的代码。
以及如何在代码中实现它
在onCreateView()
方法中添加以下内容:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater
.inflate(R.layout.fragmentwebview, container, false);
WebView webview = (WebView) view.findViewById(R.id.webView1);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient());
webview.loadUrl("https://www.google.co.uk/");
//handles the back navigation in the fragment
webView.canGoBack();
webView.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == MotionEvent.ACTION_UP
&& webView.canGoBack()) {
webView.goBack();
return true;
}
return false;
}
}); // until here
return view;
}
答案 2 :(得分:0)
在Kotlin中,您可以通过在WebView中添加setOnKeyListener来轻松处理此问题。
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val myWebView: WebView = view.findViewById(R.id.your_webView)
myWebView.webViewClient = WebViewClient()
myWebView.loadUrl("https://www.google.com/")
myWebView.setOnKeyListener { v, keyCode, event ->
if (keyCode == KeyEvent.KEYCODE_BACK && myWebView.canGoBack()) {
myWebView.goBack() // Navigate back to previous web page if there is one
nested_scroll.scrollTo(0, 0) // Scroll webview back to top of previous page
}
true
}
}