Android Webview 上的后退按钮会杀死应用程序

发布于 2025-01-03 04:47:07 字数 1410 浏览 2 评论 0原文

*已解决 我只需要公开声明我的 webview 变量,我的后退按钮类就能正常工作。感谢您的帮助,

我对 webview 类进行了独特的设置,而我在谷歌上搜索的所有内容都一无所获。

我正在使用按钮来启动 webview,但遇到的问题是,当我按后退按钮返回主应用程序屏幕时,它只会杀死该应用程序。我在底部添加了一些代码来尝试解决我的问题,但它没有起作用。

顺便说一句,下面的代码已更新为当前对我有用的代码。

package my.android.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Toast;

public class MobileAppActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

}
WebView wb;
public void onMyButtonClick01(View view)  
    {  
        Toast.makeText(this, "Pay your dues here!", Toast.LENGTH_SHORT).show(); 
        wb = new WebView(this);
        wb.loadUrl("http://www.link1.html");
        setContentView(wb);           
    }       
public void onMyButtonClick02(View view)  
{  
    Toast.makeText(this, "Re-Sign here!", Toast.LENGTH_SHORT).show(); 
    wb = new WebView(this);
    wb.loadUrl("http://www.link2.html");
    setContentView(wb);

}  
public void onBackPressed () {
    if(wb != null) {
        if(wb.canGoBack()) {
             wb.goBack();
        } else {
             setContentView(R.layout.main);
             wb = null;
        }
    } else {
        super.onBackPressed();
    }
} 
}

*Solved
I just had to publicly declare my webview variable for my back button class to work correctly. Thanks for the help

I've got a unique setup with the webview class and everything I've googled I've gotten nowhere.

I'm using buttons to launch webview's and I'm getting the issue that when I press the back button to return to my main app screen it will just kill the app. I have added some code in at the bottom to try and fix my issue but it has not worked.

btw the below code has been updated to what is currently working for me.

package my.android.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Toast;

public class MobileAppActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

}
WebView wb;
public void onMyButtonClick01(View view)  
    {  
        Toast.makeText(this, "Pay your dues here!", Toast.LENGTH_SHORT).show(); 
        wb = new WebView(this);
        wb.loadUrl("http://www.link1.html");
        setContentView(wb);           
    }       
public void onMyButtonClick02(View view)  
{  
    Toast.makeText(this, "Re-Sign here!", Toast.LENGTH_SHORT).show(); 
    wb = new WebView(this);
    wb.loadUrl("http://www.link2.html");
    setContentView(wb);

}  
public void onBackPressed () {
    if(wb != null) {
        if(wb.canGoBack()) {
             wb.goBack();
        } else {
             setContentView(R.layout.main);
             wb = null;
        }
    } else {
        super.onBackPressed();
    }
} 
}

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

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

发布评论

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

评论(1

野鹿林 2025-01-10 04:47:07

我猜您是在其他地方节省 wb 吗?不确定您如何从 onBackPressed() 访问它。假设您确实有办法获取它。

public void onBackPressed () {
    if(wb != null) {
        if(wb.canGoBack()) {
             wb.goBack();
        } else {
             setContentView(R.layout.main);
             wb = null;
        }
    } else {
        super.onBackPressed();
    }
}

I assume you're saving off wb elsewhere? Not sure how you're accessing that from onBackPressed(). Let's say you do have a way to fetch it.

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