0

I have an app that uses TabHost and loads the different activities into the TabContent area of the layout. If I click on a tab, a new intent is fired off that starts the activity. All is working well, however I'm curious how I can handle the following. Basically if I click for example contacts, the contacts.java class is loaded and my contacts.xml layout file is used for setContentView. However, if I click a button inside of my contacts.xml layout, say "Admin Contact", I need the intent to load within that same tabcontent, and it seems to just open as it's own intent and not within the tabcontent area. Is there something I'm missing with the way TabHost is meant to work? Should I have one contacts.java class and simply set different content views instead of actually starting a new activity? I have a header image and the tabs defined in my main.xml layout and my main.java is the activity which contains the tabhost. I hope my question is clear, any help is greatly appreciated!!!

Phil
  • 4,029
  • 9
  • 62
  • 107

2 Answers2

0

As Eclipse is probably telling you, TabHost and all the related tabular commands/functions are deprecated. With that disclaimer out of the way, let me tell you that I'm still using it for my application!

I am under the impression that using tabs was so that you could have one activity, but multiple views. That doesn't mean you have to have only one .java.

To switch between tabs, you should only have to do tabHost.setCurrentTab(int);

That is, assuming you set up your tabs already.

Steven_BDawg
  • 796
  • 6
  • 21
  • Yes, my tabs are working correctly, however if I click on one of my tabs, and then click on a button within the tabcontent area, the activity that starts is not loaded inside the tabcontent area, it is loaded without the tabs at the bottom and the head image I have set up at the top. Is there anyway to force a intent to open in a specific tabcontent area ? – Phil May 07 '13 at 18:55
  • It sounds like you are trying to do this perhaps :http://stackoverflow.com/questions/1306689/launching-activities-within-a-tab-in-android If so, great, because there is a solution or two that may work for you! You SHOULD be able to specify a window for the activity to run in. – Steven_BDawg May 07 '13 at 19:15
0

http://www.mkyong.com/android/android-tablayout-example/ check this example and then for each parent Activity i.e If you have a tab with contacts and profile. Then contacts and profile will be your parent activity.So extend that parent activity to ActivityGroup and make a static context of that activity and for calling an intent in that activity use

 Intent intent = new Intent(ctx, c);
    View subView = getLocalActivityManager().startActivity(id,
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
    replaceView(subView);
}

public void replaceView(View subView) {
    viewStackList.add(subView);
    setContentView(subView);
}

where c is the class you want to switch to and viewStackList is an arraylist of type View. this arraylist is used for maintaing stack for back clicks.

newBie
  • 118
  • 11
  • Thanks newBie, your code is basically the same approach I took to resolve my problem!! – Phil May 10 '13 at 18:52