Inside a fragment, I'm using WebView . I simply query 2 options when Back Button Clicked
First if; url.equals("www.google.com") [which works perfectly]
Second if; !url.equals("www.google.com") [doesn't work]
I worked in debug mode, but my second if doesn't work, even though url is different than "google.com" and closes the app!
Fragment.Java
@Override
public void onResume() {
super.onResume();
if (getView() == null) {
return;
}
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
if(url.equals("https://www.google.com")){
//normal geri
final AlertDialog.Builder builder_question1 = new AlertDialog.Builder(getContext());
builder_question1.setTitle("Are you sure to close the app?");
builder_question1.setCancelable(true);
//POSITIVE BUTTON
builder_question1.setPositiveButton("yes",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface, int i) {
System.exit(0);
}
});
//NEGATIVE BUTTON
builder_question1.setNegativeButton("no",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//do something...
//and dismiss
dialogInterface.dismiss();
}
});
builder_question1.create().show();
}
if(!url.equals("https://www.google.com")){
if(webView.canGoBack()){
webView.goBack();
}
}
}
return true;
}
});
}
WebViewClient
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Toast.makeText(getContext(), ""+url, Toast.LENGTH_SHORT).show();
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, request);
}
@Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
@Override
public void onPageFinished(WebView view, String url) {
// Toast.makeText(getContext(), "page loaded", Toast.LENGTH_SHORT).show();
super.onPageFinished(view, url);
}
});
webView.loadUrl(url);
在包含片段的活动中应用“后退”按钮
it's one of an issue, the application gets crash for a second time because of wrong context type in dialog builder when you try to display the dialog.
Change the AlertDialog context from getContext()
to getActivity();
and System.exit(0);
with getActivity().finish();
final AlertDialog.Builder builder_question1 = new AlertDialog.Builder(getActivity());
builder_question1.setTitle("Are you sure to close the app?");
builder_question1.setCancelable(true);
//POSITIVE BUTTON
builder_question1.setPositiveButton("yes",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface, int i) {
getActivity().finish();
}
});
if(!url.equals("https://www.google.com")){
if(webView.canGoBack()){
webView.goBack();
return false;
}
}
Maybe this helps you
if(url.equals("https://www.google.com")){
//normal geri
} else {
if(webView.canGoBack()){
webView.goBack();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.