Problem with a webview

1

I have a problem with WebView , the design of my application uses Tabs and Navigation Drawer , in the Tabs I have fragments that show content by webview , the problem is that each that change of Tabs the webview are updated again, is there any way to avoid it?

The image assumes that my content of the "Agreements" Tab was already loaded but when you return to that Tab it reloads as if it were the first time it opens.

This is the adapter code:

public class TabAdapter extends FragmentStatePagerAdapter {

    private Context context;

    public TabAdapter(FragmentManager manager, Context context) {
        super(manager);
        this.context = context;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0: return new BlogFragment();
            case 1: return new ConveniosFragment();
            case 2: return new FavoritosFragment();
        }
        return null;
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0: return context.getString(R.string.blog);
            case 1: return context.getString(R.string.convenios);
            case 2: return context.getString(R.string.favoritos);
        }
        return super.getPageTitle(position);
    }
}

This is the code of ConveniosFragment();

public class ConveniosFragment extends Fragment {
    private ProgressDialog progress;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //return inflater.inflate(R.layout.conveniosfragment, container, false);

        View mainView = (View) inflater.inflate(R.layout.conveniosfragment, container, false);
        WebView webView = (WebView)mainView.findViewById(R.id.viewConvenios);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setBuiltInZoomControls(false);
        webView.getSettings().setSupportZoom(false);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ELSE_NETWORK );
        webView.getSettings().setAppCacheMaxSize (1024 * 1024 * 8 );
        webView.canGoBack();
        webView.goBack();
        webView.loadUrl("http://seccion15.org.mx/convenios/");

        progress = ProgressDialog.show(getContext(), "Espere...",
                "Cargando contenido.", true);
        webView.setWebViewClient(new WebViewClient() {

            public void onPageFinished(WebView view, String url) {
                if (progress != null)
                    progress.dismiss();
            }
        });
        return mainView;


    }



    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onDetach() {
        super.onDetach();

    }

}
    
asked by Kike hatake 07.11.2017 в 21:13
source

2 answers

1

The problem is that you are creating a new fragment every time you change pages. To solve your problem, create a ArrayList of type Fragment that contains all the fragments you use in ViewPager . Then in the getItem() method you get the ArrayList fragments, using the position parameter, and return them.

  

In the code that you added, you did not put the activity that contains the ViewPager, so I'll get to the idea that it's the activity MainActivity .

public class MainActivity extends AppCompatActivity {

    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ...

        // Creas los fragmentos
        BlogFragment blogFragment  = new BlogFragment();
        ConveniosFragment conveniosFragment =  new ConveniosFragment();
        FavoritosFragment favoritosFragment = new FavoritosFragment();

        // Creas el ArrayList que contendrá los diferentes fragmentos
        ArrayList<Fragment> fragmentos = new ArrayList<Fragment>();
        fragmentos.add(blogFragment);
        fragmentos.add(conveniosFragment);
        fragmentos.add(favoritosFragment);

        ...

        // Pasas el ArrayList fragmentos como parámetro al constructor
        // del PageAdapter.
        TabAdapter tabAdapter = new TabAdapter(getSupportFragmentManager(), fragmentos);
        ...

    }

    ...

    public class TabAdapter extends FragmentStatePagerAdapter {

        private Context context;
        private ArrayList<Fragment> fragmentos;

        // Recibe la lista de fragmentos que se mostraran el ViewPager.
        public TabAdapter(FragmentManager manager, Context context, ArrayList<Fragment> fragmentos) {
            super(manager);
            this.context = context;

            // Le asignas el ArrayList fragmentos a la variable fragmentos.
            this.fragmentos = fragmentos;
        }

        @Override
        public Fragment getItem(int position) {

            // Obtienes el fragmento que se encuentra en la posición
            // de la pagina del ViewPager y lo retornas.
            return fragmentos.get(position);
        }

        ...

    }
}
  

The three points ... mean: existence of more lines of code.

I have modified the implementation of your code a bit, but I assure you, in this way it is better implemented. Now you can have more control over the different fragments, control that you did not have before.

    
answered by 08.11.2017 в 00:10
0

Actually it is an expected behavior, since every time you change tab loads a Fragment , in this case when you load the Fragment that contains the WebView reload.

To avoid reloading you can use setRetainInstance (true) :

  

setRetainInstance (boolean retain) Controls whether a fragment instance is   retains in the recreation of the activity (for example, from a change   configuration).

Add setRetainInstance(true); within onCreate() of your Fragment :

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setRetainInstance(true);
        ...
        ...
        ...
    }
    
answered by 07.11.2017 в 23:55