WebView 的问题...返回到之前的活动

发布于 2024-12-05 12:41:25 字数 2784 浏览 0 评论 0原文

我有 3 个活动 - A、B 和 C。我有一个关于活动 A 的列表,如果单击它,它会转到活动 B。在活动 B 上,我有一个按钮可以打开活动 C(Web 查看器),其中包含以下内容代码:

buyButton = (Button) findViewById(R.id.buyButton);

    buyButton.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v)
        {
           System.out.println("!!! buy !!!");

         /*Uri uri = Uri.parse("http://www.google.com");
         Intent intent = new Intent(Intent.ACTION_VIEW, uri);
         startActivity(intent);*/

            Intent startActivity = new Intent(getApplicationContext(), WebViewer.class);
            startActivity.putExtra("link","http://www.google.com");
            startActivityForResult(startActivity, 15);

        }
    });

现在,当我按下 Web 查看器活动 C 上的后退按钮时,活动 B 也会关闭。我不知道为什么会发生这种情况。有什么提示吗?谢谢。

网络查看器代码:

public class WebViewer extends Activity {



ImageButton backButton;


@Override
public void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.streams);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title3);

    String url = "";
    Bundle extras = getIntent().getExtras();
    if(extras !=null) {
        url = extras.getString("link");
    }

    if (!url.startsWith("http://") && !url.startsWith("https://"))
           url = "http://" + url;

    final TextView title=(TextView) findViewById(R.id.title_text_view_success3);
    //title.setText("Saved Streams");

    backButton = (ImageButton) findViewById(R.id.back_button3);

    backButton.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v)
        {
           System.out.println("!!! BACK !!!");
           //finishActivity(0);
           onBackPressed();
        }
    });

    setContentView(R.layout.viewer);
    final WebView webView = (WebView) findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);

    webView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress)
        {
            title.setText("Loading...");
            WebViewer.this.setProgress(progress * 100);

            if(progress == 100)
            title.setText(webView.getTitle());
        }
    });

    webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
        {
            // Handle the error
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            view.loadUrl(url);
            return true;
        }
    });

    webView.loadUrl(url);


 }



}

I have 3 activities- A, B and C. I have a list on activity A and if I click on it, it goes to activity B. On activity B, I have a button which opens activity C (web viewer) with the following code :

buyButton = (Button) findViewById(R.id.buyButton);

    buyButton.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v)
        {
           System.out.println("!!! buy !!!");

         /*Uri uri = Uri.parse("http://www.google.com");
         Intent intent = new Intent(Intent.ACTION_VIEW, uri);
         startActivity(intent);*/

            Intent startActivity = new Intent(getApplicationContext(), WebViewer.class);
            startActivity.putExtra("link","http://www.google.com");
            startActivityForResult(startActivity, 15);

        }
    });

Now, when I press the back button on web viewer activity C, activity B also closes. I am not sure why that is happening. Any hints ? Thanks.

webviewer code :

public class WebViewer extends Activity {



ImageButton backButton;


@Override
public void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.streams);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title3);

    String url = "";
    Bundle extras = getIntent().getExtras();
    if(extras !=null) {
        url = extras.getString("link");
    }

    if (!url.startsWith("http://") && !url.startsWith("https://"))
           url = "http://" + url;

    final TextView title=(TextView) findViewById(R.id.title_text_view_success3);
    //title.setText("Saved Streams");

    backButton = (ImageButton) findViewById(R.id.back_button3);

    backButton.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v)
        {
           System.out.println("!!! BACK !!!");
           //finishActivity(0);
           onBackPressed();
        }
    });

    setContentView(R.layout.viewer);
    final WebView webView = (WebView) findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);

    webView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress)
        {
            title.setText("Loading...");
            WebViewer.this.setProgress(progress * 100);

            if(progress == 100)
            title.setText(webView.getTitle());
        }
    });

    webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
        {
            // Handle the error
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            view.loadUrl(url);
            return true;
        }
    });

    webView.loadUrl(url);


 }



}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一瞬间的火花 2024-12-12 12:41:25

更改您的代码,如下所示,

backButton.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v)
        {

if(webView.canGoBack()){
           System.out.println("!!! BACK !!!");

           webView.goBack();
}
        }
    });

change your code like below,

backButton.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v)
        {

if(webView.canGoBack()){
           System.out.println("!!! BACK !!!");

           webView.goBack();
}
        }
    });
徒留西风 2024-12-12 12:41:25

首先,我认为在创建 Intent 时不应该使用 getApplicationContext。您应该只使用对当前所在活动的引用。这样 Android 将结果返回给该活动。

例子:

Intent startActivity = new Intent(MyActivity.this, WebViewer.class);

Well for one I don't think you should use getApplicationContext when creating an Intent. You should just use a reference to the activity you are currently in. That way Android returns the result to that activity.

Example:

Intent startActivity = new Intent(MyActivity.this, WebViewer.class);
冷清清 2024-12-12 12:41:25

我不确定我是否理解你的问题。但如果您想返回,请使用下面的代码。我在网络查看器中使用此代码。

@Override
    public void onBackPressed() {
        if (mainWebView.canGoBack() == true) {
            mainWebView.goBack();
        } else {
            finish();
        }
    }

I am not sure if I understand your question. but if you want to go back then use the code below. I use this code in webviewer.

@Override
    public void onBackPressed() {
        if (mainWebView.canGoBack() == true) {
            mainWebView.goBack();
        } else {
            finish();
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文