4
我知道有很多關於此主題的問題,但無法在我的應用程序上使用它。我的問題是。我有一個帶有4個選項卡的應用程序。其中3人有webView
。每當我改變標籤時,整個fragment
只是重新加載。我希望它再次加載它。所以當我失去互聯網連接時,它仍會顯示已打開的頁面。更改標籤時避免/防止刷新片段
這是我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);
}
這是我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();
}
我嘗試了很多東西...請幫助。
如果您想對數據進行加載剛有一次(在改變標籤的時候不會受到影響),你可以在onAttach()中加載它,這只是將片段附加到活動時調用的一次。 – Mahm00d
@ Mahm00d我懷疑ontach()會起作用。在onCreate()和onCreateView()之前調用attach。因此大多數查看項目不會運行。 –
@KennyWest,你是對的。儘管視圖獨立邏輯可以在onAttach()中完成,但是我再次查看他的代碼時,對於webview,onAttach()將不是解決方案。感謝您指出。 – Mahm00d