19

Following situation: I have TabActivity with e.g. three tabs, TabA, TabB, TabC.

There are a button in activity (Act_C_1) of TabC. So if the user clicks on that button, another activity (Act_C_2) should occur in TabC.

I thank you in advance for any suggestions / or ideas.

Mur

UPD:

Here is my code

TabActivity with three Activities:

public class TabScreen extends TabActivity
{
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.tab_menu);

        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        intent = new Intent().setClass(this, SecondActivity.class);

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("tab_1").setIndicator("Tab1",null).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, ThirdActivity.class);
        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("tab_2").setIndicator("Tab2",null).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, FourthActivity.class);
        spec = tabHost.newTabSpec("tab_3").setIndicator("Tab3",null).setContent(intent);
        tabHost.addTab(spec);
    }

}

Activity 'Act_C_1' or FourthActivity.java:

public class FourthActivity extends Activity implements OnClickListener
{
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fourth);

        Button BtnWeiter = (Button)findViewById(R.id.BtnWeiter);
        BtnWeiter.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) 
    {                    
        // I also tried to use LocalActivityManager
        // TabActivity parentTabActivity = (TabActivity) getParent();            
        // LocalActivityManager manager = parentTabActivity.getLocalActivityManager();
        // manager.destroyActivity("tab_3", true);
        // manager.startActivity("tab_3", new Intent(this, FourthActivity.class));
        finish();
        startActivity(new Intent(this, FourthActivity.class));            
    }        
}
Tima
  • 12,765
  • 23
  • 82
  • 125
  • 1
    Please check this link : [Android : How to have multiple activities under a single tab of TabActivity](http://gamma-point.com/content/android-how-have-multiple-activities-under-single-tab-tabactivity) – Kannan Suresh Nov 25 '11 at 04:47

4 Answers4

24

The Activities in the tab can be switched in the following manner.

First Let us understand the flow:

  1. We have in a Tab host , activity (say a list) from which we need to go to the next Activity (say details for the clicked item) under the same tab. For this we can use the concept of replacing the activity.Also setting the flags for the tab selected and other for knowing that details are being shown now

  2. When we press back we should get the previous activity under the same tab.For this instead of again replacing the activity we can refresh the tab while using the particular flag for tab which was selected. Also if flag for show details is true we'll go the the list in the same tab or else we will go the activity before the tabwidget (normal use of onBackPressed)

The code can be as follows..

  1. For going from list to details...

(This can be in the onClickListener)

private OnClickListener textListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        Constants.SHOW_DETAILS = true;
        Intent intent = new Intent(context, DetailsActivity.class);
        replaceContentView("activity3", intent);
        }
};

public void replaceContentView(String id, Intent newIntent) {
    View view = ((ActivityGroup) context)
            .getLocalActivityManager()
            .startActivity(id,
                    newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
            .getDecorView();
    ((Activity) context).setContentView(view);

}
  1. When back pressed is done we override on BackPressed in each of the Activity under the tab to go to the list again from the details screen

    @Override
      public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    if (MathHelper.SHOW_DETAILS) {
        Log.e("back", "pressed accepted");
        Constants.LIST_ACTIVITY = 1;
        Constants.SHOW_DETAILS = false;
        Intent intent = new Intent(this, Tab_widget.class);
        startActivity(intent);
        finish();
      }
     }
    

The most important part here is Constants.LIST_ACTIVITY = 1; it indicates which tab we are in. so the corresponding activities will have its value as 0,1,2...etc

Again to load the correct list (Activty) when the tab activity is refreshed we have to include this in the TabWidget onCreate after the creation of the tabs

tabHost.setCurrentTab(Constants.LIST_ACTIVITY);
Vicky Kapadia
  • 6,025
  • 2
  • 24
  • 30
  • 2
    +1, just a notice - 'context' above refers to the contained activity, not to the container (TabActivity). Oh and I do wonder why not following Android intent design and use Constants.LIST_ACTIVITY instead.. – kellogs Aug 13 '11 at 22:43
  • ya correct the context refers to the contained activities comprising the tabs. – Vicky Kapadia Aug 16 '11 at 09:33
  • I disagree, context *is* the TabActivity, otherwise you cannot cast it to an ActivityGroup. – Benoit Duffez Aug 17 '11 at 09:48
  • 1
    Cannot edit anymore, sorry. Use (ActivityGroup ((Activity) context).getParent()).getLocalActivityManager() – Benoit Duffez Aug 17 '11 at 09:57
  • Hey Bicou , actually I had 4 activities under the tab and each of them instead of extending it by activity , I have extended ActivityGroup. So context is for the inner activities and not the TabActivity. Again we are replacing the view of the inner activity with new one (Here of DetailsActivity.class) . So again this shows that it cannot be the TabActivity. Again context refers to that of the inner Activity so no need to edit it and cast by (Activity) then ( ActvityGroup) . only the latter one is sufficient. – Vicky Kapadia Aug 19 '11 at 09:28
  • if you don't mind me asking.. how do you declare Constants.LIST_ACTIVITY when i try to put it on my code.. not while running but while coding... – lemoncodes Aug 27 '12 at 03:18
  • Constants.LIST_ACTVITY is a static variable in the constants class initialized with a value which indicates the default tab selected in your tab host. – Vicky Kapadia Sep 03 '12 at 05:46
0

Just use Intent.FLAG_ACTIVITY_CLEAR_TOP flag to replace an activity for necessary tab.

intent = new Intent().setClass(this, YourActivity.class);
spec   = tabHost.newTabSpec("tab1").setIndicator("Tab1",null)
        .setContent(intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
tabHost.addTab(spec);
Dimon
  • 790
  • 1
  • 10
  • 24
-1

Just add this for all tabs:

yourTabHost.getChildAt(0).setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        getTabHost().setCurrentTab(0);
    }
});
McGarnagle
  • 101,349
  • 31
  • 229
  • 260
Help
  • 9
-2

Handle the onClick event for button

finish() the activity Add startActivity method to start another activity.

Thanks

success_anil
  • 3,659
  • 3
  • 27
  • 31
  • Nice suggestion, but that doesn't work by TabActivity ... at least by me. – Tima Oct 06 '10 at 14:18
  • I included important part of my code. 'Doesn't work' isn't right, but if I do it on that way, the tab activity will be finished. What I want to have, that just an activity within tabactivity will be finished and another one will be start within tab. – Tima Oct 07 '10 at 12:28
  • Hmm .. I 'm not against your application design... As far as i know if you want to achieve the same effect... use buttons that will look like tabs and then you will have the full control on which activity you should start or finish. Tabs work in that way only that if you try to invoke new activity from the activity getting displayed inside the tab, the new activity will take up the whole screen even the tabs too. – success_anil Oct 07 '10 at 14:53
  • Looks like this just ends an activity and starts a new one, which isn't the goal. See suggestion above. – Kyle Clegg Mar 08 '12 at 00:06