Today I faced a problem with WebView
inside a Fragment
using Android Navigation Component
.
In my case, in Fragment1
there is button under WebView
. User needs to scroll down to click it. After clicking it, a new destination is opened (Fragment2
). Then, when user goes back to Fragment1
, WebView
is reloaded, and viewport is scrolled to the top again.
It's extremely annoying. User might been scrolling down for minutes, and it always throws him to the very top after coming back. Can I somehow prevent it from reloading / scrolling back to top?
Example code could be as simple as that:
class Fragment1 : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
webView.loadData(getString(R.string.some_very_long_html), "text/html", "UTF-8")
buttonBelowWebView.setOnClickListener {
navigateToFragment2()
}
}
}
If WebView
contains videos, loading might take even up to 5 seconds. It's terrible experience.
I tried adding flag to load only one time, but then WebView
is empty when navigating back from Fragment2
:
class Fragment1 : Fragment() {
var loaded = false
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
if(!loaded) {
webView.loadData(getString(R.string.some_very_long_html), "text/html", "UTF-8")
loaded = true
}
}
}
Note that in real life I use ViewModel
s and observe data from rest api, but the problem stays the same.
Also, example code I provided is written in Kotlin, but if there is solution in Java, then it's appreciated too.
Is it possible to not reload WebView
and reset scroll position when navigating back from nested destination?
This is how fragments work in their basic form (which is what the Navigation Components use)
The fragments themselves are saved, but their views are destroyed.
This is what happens in your scenario:
If you weren't using Navigation Components, this would be very easy to solve using non-hacky ways.
You could simply use .hide()
combined with .add()
on a FragmentTransaction instead of .replace()
, and the fragments views would not be destroyed.
But seeing as you're using Navigation Components, you aren't handling the fragment transactions yourself.
A workaround would be the one posted here:
https://stackoverflow.com/a/55039009/2877453
As the fragment and their variables are saved, you could save your view as a variable in the fragment.
Then check in onCreateView, if the variable is null, inflate it like normally, if not null, then return your saved view.
Use this code before change fragment:-
if (getVisibleFragment() instanceof Fragment1){
// do nothing
}else{
// replace fragment
}
public Fragment getVisibleFragment(){
FragmentManager fragmentManager = getSupportFragmentManager();
List<Fragment> fragments = fragmentManager.getFragments();
if(fragments != null){
for(Fragment fragment : fragments){
if(fragment != null && fragment.isVisible())
return fragment;
}
}
return null;
}
Please changes to this
class Fragment1 : Fragment() {
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
webView?.loadData(getString(R.string.some_very_long_html), "text/html", "UTF8")
}
}
give a try
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With