2011-04-27 59 views
0

我想了解如何使用意圖在選項卡之間切換。使用意圖在Android選項卡之間切換

對我來說,我使用了兩個標籤:

Resources res = getResources(); 
TabHost tabHost = getTabHost(); 
TabHost.TabSpec spec; 

// Capture tab 
spec = tabHost.newTabSpec("capture").setIndicator(null, 
    res.getDrawable(R.drawable.ic_tab_capture)) 
    .setContent(new Intent(this,CaptureActivity.class)); 
tabHost.addTab(spec); 

// Upload tab 
spec = tabHost.newTabSpec("upload").setIndicator(null, 
    res.getDrawable(R.drawable.ic_tab_capture)) 
    .setContent(new Intent(this,ImageUpload.class)); 
tabHost.addTab(spec); 

爲了簡化我的目標,我的CaptureActivity.java包括下面的代碼:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.capture); 
    Intent intent = new Intent(this, ImageUpload.class); 
    startActivityForResult(intent, 0); 
} 

我很期待是,該應用程序應該立即切換到第二個標籤(ImageUpload活動),它工作正常,但標籤本身消失。我將ImageUpload活動作爲獨立頁面,而不是在選項卡本身內。

有什麼想法發生了什麼錯?

+0

ImageUpload活動不會被強制存在於選項卡中。如果您通過Intent進行切換,則不會啓動TabActivity上的選項卡,而是單獨啓動它。考慮其他機制在你的標籤之間切換,如[this](http://stackoverflow.com/questions/5430759/tab-navigation-android/5430970#5430970)之一。 – FrVaBe 2011-04-27 13:12:51

+0

啊,好的。涼。上述方式工作正常。我改變了我的計劃:事實上CaptureActivity將我重定向到非標籤的ImageUpload似乎適合我的GUI計劃。但是現在我遇到了下一個麻煩;-)我怎樣才能將非標籤式活動重定向到標籤?在我的情況下,從ImageUpload到CaptureActivity? – ctp 2011-04-27 13:27:27

+0

如果您來自Tab,只需在您的非選項卡式活動中調用[finish()](http://developer.android.com/reference/android/app/Activity.html#finish%28%29)即可取回。 – FrVaBe 2011-04-27 13:50:20

回答

1

首先調用射擊ImageUpload.java只激發ImageUpload.class,但是當然tabhost會消失。

你需要火,你加入你的兩個tabHost您的MainActivity-TabActivity

1.ImageUpload

2.CaptureActivity

,這將維持您的TabLayout

通話像這樣的意圖


Intent i=new Intent(getApplicationContext(),MainActivity.class)//which is your mainActivity-Launcher 
i.addFlags(Intent.FLAG_ACTIVITY_BRING_TO_FRONT); 
startActivity(i);//will bring MainACtivity to Front 

現在的主要活動是已經內部MainActivity

運行,所以指針直接進入onNewIntent()主要活動


覆蓋內onNewIntent()========= ============================

public class MainActivity extends TabActivty 
{ 
pubilc static TabHost tabhost; 
public void onCreate() 
{ 
    //where you added your two tab spec 
    //added them 
} 
public void onNewIntent(intent) 
{ 
    super.onNewIntent(intent); 
    tabHost.setCurrentTab(1);//1-depends where you want to switch-tabIndexno, I assume Upload is at 2nd position (so "1") 
    Activity currentActivity=getCurrentActivity(); 
      if(currentActivity instanceof Upload) 
      { 
       ((upload)currentActivity).onCreate();//watever method you want to call 
      } 
    } 

} 
0

在創建tabhost的父活動類中,我實現了泰德類似下面的方法:

public void switchTab(int tab){ 
     tabHost.setCurrentTab(tab); 
} 

裏面的標籤,我想能夠在內部切換爲我創造了下面的方法的另一個選項卡:

public void switchTabInActivity(int indexTabToSwitchTo){ 
     YOURTABHOSTACTIVITY ParentActivity; 
     ParentActivity = (YOURTABHOSTACTIVITY) this.getParent(); 
     ParentActivity.switchTab(indexTabToSwitchTo); 
} 

如果你想一個這個代碼很好的例子,你可以看看我的MintTrack項目herehere

相關問題