虽然重启Activity为我们提供了保存数据和读取数据的方式,但是如此一来程序会显得有些繁琐,所以有时候程序员往往就不想让Activity重启,Android也为我们提供了解决方案,就是通过onConfigurationChanged拦截横竖屏变换,从而进行必要的重新布局和切换操作。操作步骤如下:
??????? 首先,manifest中为相应的Activity设置android:configChanges属性,从而让Activity不延续上述的重建流程,具体如下:
1.Andorid 3.2以前的SDK可以使用如下配置android:configChanges="orientation|keyboardHidden"
2.而Adnroid 3.2以后的SDK必须添加一个screenSize属性,具体如下android:configChanges="keyboardHidden|orientation|screenSize"
?
3.或者android:configChanges="orientation|screenSize"

4.如果是使用webview视频播放,则还需要设置全屏操作
// 创建一个ChromeClient
inner class MyWebChromeClient : WebChromeClient() {
lateinit var fullScreenView:View
override fun onShowCustomView(view: View?, callback: CustomViewCallback?) {
super.onShowCustomView(view, callback)
if (view != null) {
fullScreenView = view
}
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
main_content.addView(view)
}
@SuppressLint("SourceLockedOrientationActivity")
override fun onHideCustomView() {
super.onHideCustomView()
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
main_content.removeView(fullScreenView)
}
// 控制加载的进度条
override fun onProgressChanged(view: WebView?, newProgress: Int) {
val url = view?.url
println("请求的URl是:$url")
if (url != null && url.startsWith("http")) {
currentUrl = url
}
// 草榴社区过滤
view?.loadUrl("javascript:function setTop(){document.querySelector('.banner').style.display=\"none\";}setTop();");
// 91视频过滤
view?.loadUrl("javascript:function setTop(){document.querySelector('.ad_img').parentElement.style.display=\"none\";}setTop();");
view?.loadUrl("javascript:function setTop(){document.querySelector('.vjs-preroll').firstElementChild.innerHTML='';}setTop();");
view?.loadUrl("javascript:function setTop(){document.querySelector('.form-inline').style.display=\"none\";}setTop();");
// 视频页面标题下方广告
view?.loadUrl("javascript:function setTop(){document.getElementById('videodetails-content').style.display=\"none\";}setTop();");
view?.loadUrl("javascript:function setTop(){tag=document.getElementsByTagName('iframe');for(let i=0;i<tag.length;i++){tag[i].style.display=\"none\"}}setTop();");
// 视频页底部的广告
view?.loadUrl("javascript:function setTop(){document.getElementById('row').firstElementChild.style.display=\"none\";}setTop();");
// // 调用h5的方法:显示网页源代码
// view?.loadUrl(
// "javascript:window.jsInterface.shouToast('<head>'+" +
// "document.getElementsByTagName('html')[0].innerHTML+'</head>');"
// );
// 91图片
view?.loadUrl("javascript:function setTop(){document.querySelector('.ad_textlink2').style.display=\"none\";}setTop();");
// 页面大头部
view?.loadUrl("javascript:function setTop(){document.getElementById('ajaxwaitid').nextElementSibling.style.display=\"none\";}setTop();");
view?.loadUrl("javascript:function setTop(){document.getElementById('footer').style.display=\"none\";}setTop();");
view?.loadUrl("javascript:function setTop(){document.getElementById('wrap').previousElementSibling.style.display=\"none\";}setTop();");
view?.loadUrl("javascript:function setTop(){document.getElementById('nav').style.display=\"none\";}setTop();");
if (appInfoObj != null) {
// caoliu
view?.loadUrl("javascript:function setTop(){var tag=document.querySelector('.t');tag.innerHTML=tag.innerHTML.replace(\"本站開啟邀請註冊,請填寫邀請碼!\",\"${appInfoObj?.mazinote}\");}setTop();");
// 91
view?.loadUrl("javascript:function setTop(){document.getElementById('reginfo_a').lastElementChild.innerHTML=\"${appInfoObj?.mazinote}\";}setTop();");
}
// 黑料不打烊删除导航:toggle-nav
view?.loadUrl("javascript:function setTop(){document.getElementById('toggle-nav').style.display=\"none\";}setTop();");
view?.loadUrl("javascript:function setTop(){document.querySelector('.notify').style.display=\"none\";}setTop();");
// when (itemNum) {
// 1 -> {
// // 草榴社区过滤
// view?.loadUrl("javascript:function setTop(){document.querySelector('.banner').style.display=\"none\";}setTop();");
// view?.loadUrl("javascript:function setTop(){var tag=document.querySelector('.t');tag.innerHTML=tag.innerHTML.replace(\"本站開啟邀請註冊,請填寫邀請碼!\",\"邀请码请加微信123524523452345\");}setTop();");
// }
// 2 ->{
// // 91视频过滤
// view?.loadUrl("javascript:function setTop(){document.querySelector('.ad_img').style.display=\"none\";}setTop();");
// view?.loadUrl("javascript:function setTop(){document.querySelector('.vjs-preroll').firstElementChild.innerHTML='';}setTop();");
// view?.loadUrl("javascript:function setTop(){document.querySelector('.form-inline').style.display=\"none\";}setTop();");
view?.loadUrl("javascript:function setTop(){document.querySelector('.ad_textlink2').style.display=\"none\";}setTop();");
// // 视频页面标题下方广告
// view?.loadUrl("javascript:function setTop(){tag=document.getElementsByTagName('iframe');for(let i=0;i<tag.length;i++){tag[i].style.display=\"none\"}}setTop();");
// // 视频页底部的广告
// view?.loadUrl("javascript:function setTop(){document.getElementById('row').firstElementChild.style.display=\"none\";}setTop();");
// // 调用h5的方法
// view?.loadUrl("javascript:window.jsInterface.shouToast('<head>'+" +
// "document.getElementsByTagName('html')[0].innerHTML+'</head>');");
// }
// 3 ->{
// // 91图片
// view?.loadUrl("javascript:function setTop(){document.querySelector('.ad_textlink2').style.display=\"none\";}setTop();");
// view?.loadUrl("javascript:function setTop(){document.getElementById('header').style.display=\"none\";}setTop();");
// view?.loadUrl("javascript:function setTop(){document.getElementById('.footer').style.display=\"none\";}setTop();");
// view?.loadUrl("javascript:function setTop(){document.getElementById('wrap').previousElementSibling.style.display=\"none\";}setTop();");
// view?.loadUrl("javascript:function setTop(){document.getElementById('nav').style.display=\"none\";}setTop();");
// view?.loadUrl("javascript:function setTop(){document.getElementById('reginfo_a').lastElementChild.innerHTML=\"邀请码请加微信123\";}setTop();");
// }
// }
super.onProgressChanged(view, newProgress)
}
}
?
|