views:

126

answers:

1

I've put a little app together that has three tabs to show three different web pages. It does work however I am bit worried I haven't got enough control over how this whole thing works. When I click a tab, I get a web page loaded (see code sample below), now when I click another tab another page loads in another view. When I go back to the first tab, the whole thing get initilized again and the page loads. Is there a way how I can control this and keep the underneeth tab's activity in its current state as long as I want (and say only "refresh" the page when it changes).

do I need to handle onPause()/onResume() methods for that or instead implement my tabs as views of a single activity (is this possible at all?)? How do I store the state of my activity to avoid re-initializing it every time?

this how activities are hooked to tabs:

intent = new Intent().setClass(this, tab_schedule.class);
  spec = tabHost.newTabSpec("Schedule").setIndicator("Schedule",
    res.getDrawable(R.drawable.tab_icon_schedule)).setContent(
    intent);
  tabHost.addTab(spec);

the tab_schedule.class does a simple web page load:

@Override
 public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  setContentView(R.layout.tab_people);

  try {

   WebView currentView = (WebView) findViewById(R.id.tab_people_WebView);
   currentView.getSettings().setJavaScriptEnabled(true);
   currentView.loadUrl("http://pda.lenta.ru");

  } catch (Exception e) {
   Log.v("webClientInit", e.getMessage());
  }
 }
+1  A: 

If you don't want to create a new activity for each tab, you can use a TabWidget with a FrameLayout to toggle between views.

As to switching activities, see this question for a way to not recreate the activity each time.

Regardless, you should always implement onPause and onResume to restore the state of your app. You might want to read up on the Activity Lifecycle, but basically you cannot prevent your activity from being killed if goes into the background. Thus, you should store your state in onPause. The link above has some info on how to do so as well.

Mayra