0

I am using FragmentTabHost and TabWidget to create 4 tabs. When some tab is selected, it opens its relevent xml file from its related class. Lets say I selected tab 1. It opened a website in a webview. Then I move to next tab i.e tab 2. When I come back to tab 1, it redraws the webview and requests again for the website. Its useless as the same page is required as before. I mean last state of tab is not preserved. It opens from scratch. How do I get the last content of webpage wherever I left the tab 1? Here is the code:

setContentView(R.layout.bottom_tabs);

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

    Bundle b = new Bundle();
    b.putString("key", "Home");
    mTabHost.addTab(mTabHost.newTabSpec("home").setIndicator("Home"),
            Home.class, b);
    //
    b = new Bundle();
    b.putString("key", "Menu");
    mTabHost.addTab(mTabHost.newTabSpec("menu").setIndicator("Menu"),
            MenuDemo.class, b);


    b = new Bundle();
    b.putString("key", "Promos");
    mTabHost.addTab(mTabHost.newTabSpec("promos").setIndicator("Promos"),
            Promos.class, b);

    b = new Bundle();
    b.putString("key", "Promos");
    mTabHost.addTab(mTabHost.newTabSpec("location").setIndicator("Location"),
            Location.class, b);

Tab 1 opens this activity:

public class Home extends Fragment{

public Home() {
    // TODO Auto-generated constructor stub

}

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
}

@SuppressLint("SetJavaScriptEnabled")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View v = LayoutInflater.from(getActivity()).inflate(R.layout.home,
            null);
    WebView webView = (WebView) v.findViewById(R.id.webViewHome);
    webView.setWebViewClient(new MyWebViewClient());
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl("http://www.google.com");

    return v;
}
private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
}
//

}

Ali Hassan
  • 519
  • 6
  • 26
  • 1
    Please refer to [this question](http://stackoverflow.com/questions/10258772/android-webview-savestate). I think it may help. – mehmetseckin Dec 11 '13 at 13:51
  • You should hide the fragment on tab clicking and when you come back to that tab show it again. – Rudi Dec 11 '13 at 13:53

1 Answers1

0

In your mainTab Activity you should do something like this to hide the fragment and check if it already loaded just show it :

public void onTabChanged(String tag) {
        TabInfo newTab = this.mapTabInfo.get(tag);
        if (mLastTab != newTab) {
            FragmentTransaction ft = this.getSupportFragmentManager()
                    .beginTransaction();
            if (mLastTab != null) {
                if (mLastTab.fragment != null) {
                    // ft.detach(mLastTab.fragment);
                    ft.hide(mLastTab.fragment);
                }
            }
            if (newTab != null) {
                if (newTab.fragment == null) {
                    newTab.fragment = Fragment.instantiate(this,
                            newTab.clss.getName(), newTab.args);
                    ft.add(R.id.realtabcontent, newTab.fragment, newTab.tag);
                } else {

                    // ft.attach(newTab.fragment);
                    ft.show(newTab.fragment);

                }
            }

            mLastTab = newTab;
            ft.commit();
            this.getSupportFragmentManager().executePendingTransactions();

        }
    }

and send the tab's tag to that.

Hope it helps :)

Rudi
  • 4,304
  • 4
  • 34
  • 44