4

I know there are a lot of questions about this subject but I can't get it working on my application. My problem is. I have an App with 4 Tabs. In 3 of them there is a webView. Every time I change Tabs the whole fragment just reloads. I want it to load it again. So when I lost my internet connection it will still show the page that was open.

This is my mainactivity:

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);
}

And this 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.

Hybrid Developer
  • 2,320
  • 1
  • 34
  • 55
  • If you want the data to be loaded just one time (not affected while changing tabs), you can load it in `onAttach()` which is called just one time at attaching the fragments to the activity. – Mahm00d May 03 '14 at 20:19
  • @Mahm00d I doubt onAttach() will work. On attach is called before onCreate() and onCreateView(). Therefore most view items will not run. – Kennedy Nyaga Oct 09 '14 at 13:36
  • @KennyWest, you're right. Although the view independent logic can be done in onAttach(), as I looked at his code again, for webview here onAttach() won't be a solution. Thanks for pointing it out. – Mahm00d Oct 09 '14 at 18:14

1 Answers1

0

I know it's a crazy workaround, but if you're trying to give the user "last viewed HTML" maybe you should consider caching the HTML that was successfully downloaded, and then set this HTML back to the WebView if downloading again fails?

Preventing the reloading is probably better, but in case you don't find anything else, check this link to find how to take a copy of the downloaded HTML: how to get html content from a webview?

Community
  • 1
  • 1
Ofer Lando
  • 814
  • 7
  • 12