Java – Avoid/prevent fragment from refreshing when changing tabs

Avoid/prevent fragment from refreshing when changing tabs… here is a solution to the problem.

Avoid/prevent fragment from refreshing when changing tabs

I

know there are a lot of questions on this topic, but I can’t use it in my app. My question is. I have an app with 4 tabs. 3 of them have a webView. Every time I change the tabs, the whole fragment reloads. I want it to load it again. So, when I lose my internet connection, it still shows the open page.

This is my main activity:

public class MainActivity extends FragmentActivity {

private FragmentTabHost TabHost;

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

setContentView(R.layout.activity_main);
    instellingen laden
    final SharedPreferences settings = getSharedPreferences("Leerling", 0); 

lln laden
     int lln = settings.getInt("lln", 0);   
     if(lln == 0) { 

alert maken
         AlertDialog.Builder alert = new AlertDialog.Builder(this);
         alert.setTitle("Welkom");
         alert.setMessage("Vul je LLN in:");

 EditText maken
         final EditText input = new EditText(this);
         input.setInputType(InputType.TYPE_CLASS_NUMBER);
         alert.setView(input);

ga knop
         alert.setPositiveButton("Ga", new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int whichButton) {
                 String text = input.getEditableText().toString();
                 int lln = Integer.parseInt(input.getEditableText().toString());
                 SharedPreferences.Editor editor = settings.edit();
                 editor.putInt("lln", lln);
                 editor.commit();
               }
         });
         stop knop
         alert.setNegativeButton("Stop", new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int whichButton) {
                  Canceled.
                 android.os.Process.killProcess(android.os.Process.myPid());
                 return;
             }
         });

alert.show();
     }

TabHost = (FragmentTabHost)findViewById(android. R.id.tabhost);
     TabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

tab1 toevoegen
     TabHost.addTab(TabHost.newTabSpec("tab1").setIndicator("Rooster"), Tab1.class, null);

tab2 toevoegen
     TabHost.addTab(TabHost.newTabSpec("tab2").setIndicator("Pers"), Tab2.class, null);

tab3 toevoegen
     TabHost.addTab(TabHost.newTabSpec("tab3").setIndicator("Wijzig"), Tab3.class, null);

tab4 toevoegen
     TabHost.addTab(TabHost.newTabSpec("tab4").setIndicator("Extra's"), Tab4.class, null);
}

Here is my Tab1:

public class Tab1 extends Fragment  {

private Bundle webViewBundle;
private WebView webViewRooster;

@Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     Inflate the layout for this fragment
    View View = inflater.inflate(R.layout.activity_tab1, container, false);

SharedPreferences settings = getActivity().getSharedPreferences("Leerling", 0); 
    int lln = settings.getInt("lln", 0);        

webViewRooster = (WebView) View.findViewById(R.id.webViewRooster);        
    webViewRooster.setWebViewClient(new WebViewClient());

if (webViewBundle == null) {
        Toast.makeText(getActivity(), "geladen voor eerste x", Toast.LENGTH_SHORT).show();
        webViewRooster.loadUrl("http://www.idylank.x90x.net/rooster.php?lln=" + lln);
    }
    else{
        Toast.makeText(getActivity(), "tweede of meer x", Toast.LENGTH_SHORT).show();
        webViewRooster.restoreState(webViewBundle);
    }
    return View;         
}
public void onPause() {
    webViewBundle = new Bundle();
    webViewRooster.saveState(webViewBundle);
    super.onPause();
}

I tried a lot of things… Please help.

Solution

I know this is a crazy workaround, but if you’re trying to provide the user with “last viewed HTML”, maybe you should consider caching the HTML that has been successfully downloaded, and then set this HTML back to WebView if the download fails again?

It might be better to prevent reloading, but if you don’t find anything else, check this link to learn how to get a copy of the downloaded HTML: how to get html content from a webview?

Related Problems and Solutions