2011-03-12 89 views
0
TabHost tabHost = getTabHost(); 
    TabHost.TabSpec spec; // Resusable TabSpec for each tab 
    Intent intent; // Reusable Intent for each tab 
    // Create an Intent to launch an Activity for the tab (to be reused) 
    intent = new Intent().setClass(this, FirstActivity.class); 
    // Initialize a TabSpec for each tab and add it to the TabHost 
    spec = tabHost.newTabSpec("hottest").setIndicator("Hottest", 
         res.getDrawable(R.drawable.ic_tab_hottesttrack)) 
        .setContent(intent); 
    tabHost.addTab(spec); 
    intent = new Intent().setClass(this, SecondTrackActivity.class); 
    spec = tabHost.newTabSpec("latest").setIndicator("Latest", 
         res.getDrawable(R.drawable.ic_tab_latesttrack)) 
        .setContent(intent); 
    tabHost.addTab(spec); 

Okey!我的代碼很簡單。製作2個選項卡並將它們添加到TabHost中。問題是我的兩項活動都使用了大量的記憶並需要時間來處理。一次只能加載一個活動。但是,當我開始我的應用程序,似乎兩個活動都加載,這將需要更長的時間。 因此,我希望只有一個活動允許在選擇其選項卡時加載,如果我選擇第一個選項卡,然後選擇第一個活動加載,我選擇第二個選項卡,然後第二個活動將加載,而不是同時加載。任何建議?使用TabHost切換選項卡時加載單獨的活動

回答

0

當活動開始時,它們的onCreate()onResume()回調被調用。例如,您只能開始計算。用戶按下活動內的按鈕,以便初始加載很快。 此外,還可以將計算結果放在AsyncTask之內,以便計算不會阻塞UI線程。

+0

如果我希望只有當我切換到它的選項卡時才加載特定活動(無需用戶按下按鈕),如使用setOnTabChangedListener() – ForeverNights 2011-03-12 20:57:28

相關問題