Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Navigation Component keeps reloading WebView

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 ViewModels 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?

like image 666
xinaiz Avatar asked May 13 '19 15:05

xinaiz


3 Answers

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:

  • Fragment 1 created
  • Fragment 1 view created (This contains the webview)
  • Button clicked
  • Fragment 1 view destroyed
  • Fragment 2 created
  • Fragment 2 view created
  • Back clicked
  • Fragment 1 view re-created
  • Fragment 2 view destroyed
  • Fragment 2 destroyed

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.

like image 164
Moonbloom Avatar answered Sep 17 '22 13:09

Moonbloom


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;
    }
like image 44
Bhautik Keraliya Avatar answered Sep 20 '22 13:09

Bhautik Keraliya


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

like image 23
patel dhaval r Avatar answered Sep 18 '22 13:09

patel dhaval r