0

I wish to find out how to switch between tabs using intents.

In my case I'm using the both tabs:

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);

To simplify my goal, my CaptureActivity.java includes the following code:

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

What I'm expecting is, the app should switch instantly to the second tab (ImageUpload activity) which works fine, BUT the tabs themselves disappear. I get the ImageUpload activity as stand-alone page, not within the tab itself.

Any idea what's going wrong there ?

halfer
  • 19,824
  • 17
  • 99
  • 186
ctp
  • 1,077
  • 1
  • 10
  • 28
  • The ImageUpload activity is not forced to live in a tab. If you switch by Intent you will not start the tab on your TabActivity but start it independently. Consider other mechanism to switch between your tabs like [this](http://stackoverflow.com/questions/5430759/tab-navigation-android/5430970#5430970) one. – FrVaBe Apr 27 '11 at 13:12
  • Ah, ok. Cool. The way described above works fine. I changed my plan: the fact CaptureActivity redirects me to non-tabbed ImageUpload seems to fit into my GUI plans fine. But for now I've the next trouble ;-) How can I redirect from a non-tabbed activity to a tab? In my case from ImageUpload to CaptureActivity? – ctp Apr 27 '11 at 13:27
  • If you come from a Tab just call [finish()](http://developer.android.com/reference/android/app/Activity.html#finish%28%29) in your non tabbed activity to get back. – FrVaBe Apr 27 '11 at 13:50
  • Check my answer in this post: [http://stackoverflow.com/questions/5536632/how-to-pass-the-data-through-tabs/5536811#5536811](http://stackoverflow.com/questions/5536632/how-to-pass-the-data-through-tabs/5536811#5536811) Hope this helps!! – mudit Apr 27 '11 at 13:20

2 Answers2

1

First calling firing ImageUpload.java only fires ImageUpload.class but surely tabhost will disappear.

You need to fire your MainActivity-TabActivity where you added your two tabHost

1.ImageUpload

2.CaptureActivity

which will maintain your TabLayout

call intent like these


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

Now Main activity is already running so pointer directly goes to onNewIntent() inside Main Activity


Override onNewIntent() inside MainActivity

=====================================

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
           }
  }

}
Vikalp Patel
  • 10,669
  • 6
  • 61
  • 96
0

In the parent activity class where the tabhost is created I implemented a method like the one below:

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

Inside of the tab that I would like to be able to switch internally to another tab I created the method below:

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

If you would like a good example of this code, you can take a look at my MintTrack project here and here.

Jagdeep Singh
  • 885
  • 6
  • 18