更改标签时避免/阻止片段刷新

时间:2013-10-10 13:57:44

标签: java android tabs fragment android-fragmentactivity

我知道有很多关于这个问题的问题,但我不能让它在我的应用程序上工作。我的问题是。我有一个带有4个标签的应用程序。其中3个有webView。每次我更改Tabs时,整个fragment只会重新加载。我希望它再次加载它。因此,当我丢失了我的互联网连接时,它仍会显示已打开的页面。

这是我的主要活动:

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

这是我的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();
}

我尝试了很多事情......请帮助。

1 个答案:

答案 0 :(得分:0)

我知道这是一个疯狂的解决方法,但是如果你试图给用户“最后查看的HTML”,你应该考虑缓存成功下载的HTML,然后如果再次下载失败则将此HTML设置回WebView ?

防止重新加载可能会更好,但如果您没有找到任何其他内容,请查看此链接以了解如何获取已下载HTML的副本:how to get html content from a webview?