简体   繁体   English

网站浏览后的返回按钮

[英]Back button after webview

I've got 2 activities Activity A & Activity B (webview) What i'm trying to do is, when the webview can't go back anymore, it will display Activity A and on press again prompt for exit 我有2个活动, 活动A活动B (网络视图),我想做的是,当网络视图无法再返回时,它将显示活动A,并再次按下以提示退出

Here is Activity A : 这是活动A:

import com.jeumont.app.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;



public class frontpage extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.frontpage); 
}
}

The webview which contains the backpress function : 包含backpress功能的web视图:

public class webview extends Activity {

private WebView webView;
@SuppressLint("SetJavaScriptEnabled") @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //Init Barre de chargement
    final ProgressDialog pd = ProgressDialog.show(this, "", "Chargement en cours", true);
    setContentView(R.layout.activity_webapp);
    webView = (WebView) findViewById(R.id.webView_web_app);
    webView.getSettings().setJavaScriptEnabled(true);
    String url = getIntent().getStringExtra("url");
    webView.loadUrl(url);
    webView.setWebViewClient(new WebViewClient()
    {
         //On enleve le progress quand la page est chargée
        public void onPageFinished(WebView view, String url)
              { 
                pd.dismiss();
                 super.onPageFinished(view, url);
              }

So i've tried several things to make this work. 因此,我尝试了一些方法来使此工作。

public void onBackPressed (){

    if (webView.isFocused() && webView.canGoBack()) {
            webView.goBack(); 

    }
 super.onBackPressed(); 

works as expected, the webview goes back and when it can't anymore switch back to activity A and if you press again leaves the app 可以按预期工作,Web视图将返回,并且当它无法再切换回活动A时,如果再次按下,将离开该应用程序

i'm actually using : 我实际上正在使用:

public void onBackPressed (){

    if (webView.isFocused() && webView.canGoBack()) {
            webView.goBack();       
    }

    else

    {

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Voulez vous quitter ?")
                   .setCancelable(false)
                   .setPositiveButton("Oui", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                            finish();
                       }
                   })
                   .setNegativeButton("Non", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                       }
                   });
            AlertDialog alert = builder.create();
            alert.show();

    }
}

the prompts works fine, but it pops out when the webview can't go back anymore, if yes pressed you go back to activity A 提示工作正常,但是当Webview再也无法返回时会弹出,如果按下,则返回活动A

how can i make this prompt pop when the current activity is A and not the webview? 当前活动为A而不是Webview时如何弹出此提示? i don't really know how to do this, i've heard about fragments, is that the way to do it ? 我真的不知道该怎么做,听说过碎片,那是这样做的方法吗?

hope this is clear. 希望这很清楚。

You should override onBackPressed() in both the activity. 您应该在两个活动中都覆盖onBackPressed()。

In Activity A: 在活动A中:

        @Override
        public void onBackPressed (){
                   //AlertDialog to exit the app.

        }

In Activity B: 在活动B中:

@Override
public void onBackPressed (){

        if (webView.isFocused() && webView.canGoBack()) {
                webView.goBack(); 

        }else{
              super.onBackPressed(); 
         }

    }

In your Avtivity A, override onBackPressed and put code of showing prompt: 在Avivity A中,覆盖onBackPressed并放置显示提示的代码:

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Voulez vous quitter ?")
           .setCancelable(false)
           .setPositiveButton("Oui", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   finish();
               }
           })
           .setNegativeButton("Non", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   dialog.cancel();
               }
           });
     AlertDialog alert = builder.create();
     alert.show();
}

And remove the same from webview's activity in else block. 并从else块中的webview活动中删除相同内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM