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
Hey there when ever you come in fragment first time or coming back its method onViewCreated always call again and again, if you want something like you don't want to reset you can do it by one global boolean check because that global variable will not be change after fragment resumes
Like this
var shouldLoad = true
And then what ever you want to do
if(shouldLoad){
//do your work
//setting false to your boolean
shouldLoad = false
}else{
//if you need something else
}
Try this -
this will be implemented in the fragment that has webview
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (view == null) {
// inflate your layout here
return view = inflater.inflate(R.layout.community_home_layout, container, false);
}else {
return view;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.