2011-05-06 31 views
1

我有一個具有兩個選項卡的TabHost的TabActivity。每個標籤都有自己的意圖。看起來像意圖的onResume()在檢測到標籤是否被更改之前觸發。我該如何解決這個問題?爲什麼我的Android Tab不能正常工作?

TabActivity代碼:

public class TabHostActivity extends TabActivity { 
    static final int SHOW_SHARE_ACTIVITY = 0; 
    static final int SHOW_LOGIN_ACTIVITY = 1; 

    private TabHost tabHost; 
    private ImageButton composeImageButton; 
    private SharedPreferences prefs; 
    private Bundle b; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.tabhostactivity);  
     prefs = getSharedPreferences(Constants.PREFS_NAME, 0); 
     //Setup the ActionBar 
     composeImageButton = (ImageButton) findViewById(R.id.composeImageButton); 
     composeImageButton.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if(prefs.getBoolean("isLoggedIn", false)) 
       { 
        showShareActivity(); 
       } 
       else 
       { 
        Intent intent = new Intent(TabHostActivity.this, LoginActivity.class); 
        startActivityForResult(intent, SHOW_LOGIN_ACTIVITY); 
       } 
      } 
     }); 

     b = new Bundle(); 
     //Setup the Tabs 
     Resources res = getResources(); // Resource object to get Drawables 
     tabHost = getTabHost(); // The activity TabHost 
     tabHost.setOnTabChangedListener(new OnTabChangeListener() { 
       @Override 
       public void onTabChanged(String arg0) { 
        if(tabHost.getCurrentTab() == 0) //Check if the Watchlist tab was clicked so we can prompt login 
        { 
         //Toast toast = Toast.makeText(getApplicationContext(), "TRENDING = YES", Toast.LENGTH_SHORT); 
         //toast.show(); 
         b.putBoolean("isTrendingTab",true); 
        } 
        else 
        { 
         Toast toast = Toast.makeText(getApplicationContext(), "TRENDING = NO", Toast.LENGTH_SHORT); 
         toast.show(); 
         b.putBoolean("isTrendingTab",false); 
        } 
       }  
     }); 

     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, ARActivity.class); 
     intent.putExtras(b); 

     // Initialize a TabSpec for each tab and add it to the TabHost 
     spec = tabHost.newTabSpec("trending").setIndicator("Trending",res.getDrawable(R.drawable.icon)).setContent(intent); 
     tabHost.addTab(spec); 

     // Do the same for the other tabs 
     intent = new Intent().setClass(this, WatchlistActivity.class); 
     intent.putExtras(b); 
     spec = tabHost.newTabSpec("watchlist").setIndicator("Watchlist",res.getDrawable(R.drawable.icon)).setContent(intent); 
     tabHost.addTab(spec); 

     tabHost.setCurrentTab(0); 
    } 

    private void showShareActivity() 
    { 
     Intent intent = new Intent(TabHostActivity.this, ShareActivity.class); 
     startActivityForResult(intent, SHOW_SHARE_ACTIVITY); 
    } 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if(requestCode == SHOW_LOGIN_ACTIVITY) 
     { 
      //Login was successful so lets show the compose box! 
      if (resultCode == RESULT_OK) { 
       showShareActivity(); 
      } 
     } 
    } 
} 

這裏是的onResume在Intent我的活動之一:

public void onResume() 
    { 
     super.onResume(); 

     Bundle bundle = getIntent().getExtras(); 

     if(bundle.getBoolean("isTrendingTab")) 
     { 
      Toast toast = Toast.makeText(getApplicationContext(), "TRENDING!", Toast.LENGTH_SHORT); 
      toast.show(); 
     } 
     else 
     { 
      Toast toast = Toast.makeText(getApplicationContext(), "WATCHLIST!", Toast.LENGTH_SHORT); 
      toast.show(); 
     } 
    } 
+0

你有什麼問題?是的,在您看到它之前,onResume已經開火,但您遇到什麼錯誤/問題? – AedonEtLIRA 2011-05-11 22:21:17

+0

不是最簡單的解決方案,但使用視圖而不是活動作爲標籤內容應該給你更多的控制。 – 2011-05-11 22:29:19

回答

1

如果我理解正確的問題是,你儘量把

b.putBoolean("isTrendingTab",true); 

(或虛假)您要通過檢測更改啓動的意圖。

這是錯誤的方法。

變更事件將始終發生在活動啓動後,您應該執行不同的邏輯。你必須重新考慮它。

0

你看過Activity life cycle嗎?在創建活動時也會調用簡歷,並且bundle.getBoolean(「isTrendingTab」)行沒有默認值,以防尚未設置默認值...

您可以先設置它嗎onCreate爲默認值?我認爲這是你的問題。代碼有點草率。您正試圖將變量傳遞給每個活動,但它們仍然存在於選項卡活動中。視圖會是一個更好的方法,因此它們都可以在選項卡活動中看到相同的變量。

0

您的類的ARActivity.class的oncreate將在您的標籤主機的onresume方法之前調用。

所以在ARActivity中做任何你想要的處理。

此外,由於您的tabHost.setCurrentTab(0);你的起始標籤總是ARActivity。

如果你想激活根據您的標籤改變代碼,弄清楚你是哪個選項卡上使用的主要tabhost ontabchange使用和使用id,然後發送給內廣播接收機的請求。

if (tabHost.getCurrentTab() == 0) { 
        i.setAction(getString(R.string.br_refresh_home_tab)); 
        sendBroadcast(i); 
       } else { 
        i.setAction(getString(R.string.br_refresh_sports_tab)); 
        sendBroadcast(i); 
       } 

在你ARActivity,

protected class RefreshList extends BroadcastReceiver { 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      if (intent.getAction().equals(
        getString(R.string.br_refresh_home_tab))) { 


      } 
     } 
    } 
0

有沒有必要使用onTabChanged()。

這裏是我如何做到這一點在我的應用程序(有一些粘貼在你的價值觀)。我布爾標誌添加到意圖,而不是額外的包:

Intent intent = new Intent(action) // see notes below about "action" 
    .setClass(this, ARActivity.class) 
    .putExtra("isTrendingTab", true); 
TabHost.TabSpec spec = tabHost.newTabSpec("trending") 
    .setIndicator("trending", getResources().getDrawable(drawableId)) 
    .setContent(intent); 
tabHost.addTab(spec); 

然後在的onResume():

if (getIntent().getBooleanExtra("isTrendingTab", false)) {... 

我發現,使用多個標籤同一類的時候,我不得不區分它們用不同的動作字符串中的每個意圖構造,如上所述。否則,它不會在相同類的選項卡之間切換時創建新的活動。你看起來還沒有這樣做,所以你可以繼續保留它。我想我會提到它,因爲通過isTrendingTab表明你可能正在走這條路。