Don't scroll to top when navigating back/forward in WebView

This commit is contained in:
arkon 2020-12-10 09:15:09 -05:00
parent b6e246c6b2
commit 8ffd3a8ed2
3 changed files with 23 additions and 14 deletions

View File

@ -21,8 +21,6 @@ class MyAnimeListLoginActivity : BaseWebViewActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
title = getString(R.string.login)
if (bundle == null) { if (bundle == null) {
binding.webview.webViewClient = object : WebViewClientCompat() { binding.webview.webViewClient = object : WebViewClientCompat() {
override fun shouldOverrideUrlCompat(view: WebView, url: String): Boolean { override fun shouldOverrideUrlCompat(view: WebView, url: String): Boolean {
@ -68,9 +66,10 @@ class MyAnimeListLoginActivity : BaseWebViewActivity() {
companion object { companion object {
fun newIntent(context: Context): Intent { fun newIntent(context: Context): Intent {
val intent = Intent(context, MyAnimeListLoginActivity::class.java) return Intent(context, MyAnimeListLoginActivity::class.java).apply {
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
return intent putExtra(TITLE_KEY, context.getString(R.string.login))
}
} }
} }
} }

View File

@ -23,6 +23,8 @@ open class BaseWebViewActivity : BaseActivity<WebviewActivityBinding>() {
internal var bundle: Bundle? = null internal var bundle: Bundle? = null
internal var isRefreshing: Boolean = false
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -40,6 +42,8 @@ open class BaseWebViewActivity : BaseActivity<WebviewActivityBinding>() {
finish() finish()
} }
title = intent.extras?.getString(TITLE_KEY)
setSupportActionBar(binding.toolbar) setSupportActionBar(binding.toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true) supportActionBar?.setDisplayHomeAsUpEnabled(true)
binding.toolbar.navigationClicks() binding.toolbar.navigationClicks()
@ -87,5 +91,10 @@ open class BaseWebViewActivity : BaseActivity<WebviewActivityBinding>() {
fun refreshPage() { fun refreshPage() {
binding.swipeRefresh.isRefreshing = true binding.swipeRefresh.isRefreshing = true
binding.webview.reload() binding.webview.reload()
isRefreshing = true
}
companion object {
const val TITLE_KEY = "title_key"
} }
} }

View File

@ -25,8 +25,6 @@ class WebViewActivity : BaseWebViewActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
title = intent.extras?.getString(TITLE_KEY)
if (bundle == null) { if (bundle == null) {
val url = intent.extras!!.getString(URL_KEY) ?: return val url = intent.extras!!.getString(URL_KEY) ?: return
@ -60,7 +58,10 @@ class WebViewActivity : BaseWebViewActivity() {
binding.swipeRefresh.isRefreshing = false binding.swipeRefresh.isRefreshing = false
// Reset to top when page refreshes // Reset to top when page refreshes
if (isRefreshing) {
view?.scrollTo(0, 0) view?.scrollTo(0, 0)
isRefreshing = false
}
} }
} }
@ -120,12 +121,12 @@ class WebViewActivity : BaseWebViewActivity() {
private const val TITLE_KEY = "title_key" private const val TITLE_KEY = "title_key"
fun newIntent(context: Context, url: String, sourceId: Long? = null, title: String? = null): Intent { fun newIntent(context: Context, url: String, sourceId: Long? = null, title: String? = null): Intent {
val intent = Intent(context, WebViewActivity::class.java) return Intent(context, WebViewActivity::class.java).apply {
intent.putExtra(URL_KEY, url) addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
intent.putExtra(SOURCE_KEY, sourceId) putExtra(URL_KEY, url)
intent.putExtra(TITLE_KEY, title) putExtra(SOURCE_KEY, sourceId)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) putExtra(TITLE_KEY, title)
return intent }
} }
} }
} }