/*
拿到webview 的回调给Flutter
*/
public class FlutterBrowserManager {
private static FlutterBrowserManager mManager;
// 临时登录回调,登录后即销毁
private OpenListener mOpenListener;
// 单例模式 保持单一
public static FlutterBrowserManager getInstance() {
if (mManager == null) {
synchronized (FlutterBrowserManager.class) {
if (mManager == null) {
mManager = new FlutterBrowserManager();
}
}
}
return mManager;
}
/**
* 跳转内部浏览器打开
*
* @param context
* @param url
*/
public void openBrowser(Context context, String url) {
ARouter.getInstance().build(RouterConstants.WEB_PAGE)
.withString(RouterConstants.Param.WEB_URL, url)
.navigation(context);
}
/**
* 跳转内部浏览器打开 并监听是否页面关闭
*
* @param context
* @param url
*/
public void openBrowser(Context context, String url, OpenListener listener) {
this.mOpenListener = new OpenListener() {
@Override
public void onClose() {
if (listener != null) {
Logger.e("colose","openBrowser1");
listener.onClose();
Logger.e("colose","openBrowser2");
}
}
};
ARouter.getInstance().build(RouterConstants.WEB_PAGE)
.withString(RouterConstants.Param.WEB_URL, url)
.navigation(context);
}
/**
* 获取登录监听
*
* @return
*/
public OpenListener getOpenListener() {
return mOpenListener;
}
public interface OpenListener {
//当页面关闭
void onClose();
}
}
在web页面对回调进行处理
@Override
public void onBackPressed() {
FlutterBrowserManager.OpenListener authListener = FlutterBrowserManager.getInstance().getOpenListener();
if (authListener != null) {
Logger.e("colose","onBackPressed1");
authListener.onClose();
Logger.e("colose","onBackPressed2");
}
super.onBackPressed();
}
在使用的时候就可以监听返回按键了
FlutterBrowserManager.getInstance()
.openBrowser(mContext, url, object : FlutterBrowserManager.OpenListener {
override fun onClose() {
// 监听到了返回
result.success(true);
}
})
|