javascript - 使用 Javascript 从 Web View 切换到应用程序 View

标签 javascript android jquery

在移动应用程序上,当点击用户从欢迎屏幕重定向到下一个我有一个通过 API 调用的 WebView 显示时,现在我想从该 WebView 返回到上一个屏幕但它没有发送回来。

请注意,第一个屏幕在应用内,第二个屏幕是通过 API 显示的 WebView 。

我已经从 WebView 中尝试了以下内容:

<a href="javascript:history.back();">Back</a>

最佳答案

使用 JavaScript 函数完成 webView Activity :-

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");

添加接口(interface):-

public class WebAppInterface {
Context mContext;

/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
    mContext = c;
}

/** method to finish activity */
@JavascriptInterface
public void closeActivity() {
    MainActivity.this.finish();// MainActivity is your activity name which you want to close
}
}

并且您的 HTML 和 JavaScript 应包含如下内容:-

<input type="button" value="Back" onClick="closeAndroidActivity()" />

<script type="text/javascript">
    function closeAndroidActivity() {
        Android.closeActivity();
    }
</script>

有关更多信息,请参阅 this

关于javascript - 使用 Javascript 从 Web View 切换到应用程序 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51393684/

相关文章:

javascript - Angular 重新加载 url 但不是状态 View

android - 在 Google map 上保存路线

android - SharedPreferences 重置 : Map size is not 0 after reset

javascript - 点击时随机化数字,但绝不是相同的数字

javascript - Bootstrap -btn 组,数据切换 ="buttons": how to select a button with JS and deselect all others?

javascript - geoJson 坐标的 Mongoose 模式

javascript - 如何在node.js中生成进程时记录信息?

jQuery 验证动态输入

javascript - 如何根据对象中其他键的值自动将值分配给另一个键

android - 在 ScrollView 屏幕底部定位按钮并设置比例宽度