0

I need to implement tabs in my project.i have a layout, in which i have two tabs and a button. For two tabs,I have two activities and button calling different activity. the thing is I am showing result of button on first tab. now what i want is,if i press to first tab, first tab intent should be shown. or if i change tabs, tab's corresponding intent should be shown.

I tried the following code, but after clicking on button, every time i click on tab1, the intent of button shown

public class TabLayoutUsingTabChangeEventActivity extends TabActivity {

        @Override
        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);                
            final TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
            final TabHost.TabSpec sp1 = tabHost.newTabSpec("TAB1");
            TabHost.TabSpec sp2 = tabHost.newTabSpec("TAB2");

            //Creating First Tab
            Intent intent1 = new Intent(this, Tab1Activity.class);
            sp1.setIndicator("TAB1").setContent(intent1);
            tabHost.addTab(sp1);

            //Creating Second Tab 
            Intent intent2 = new Intent(this, Tab2Activity.class);
            sp2.setIndicator("TAB2").setContent(intent2);
            tabHost.addTab(sp2);               

            //Tab Changed Event
            tabHost.setOnTabChangedListener(new OnTabChangeListener(){
                 @Override
                 public void onTabChanged(String tabId) {
                     Log.i("TabId :", tabId);
                     if(tabId.equals("TAB1")){
                     Log.i("TAB1", "TAB1 Changed");
                     Intent intent1 = new Intent().setClass(getApplicationContext(), Tab3Activity.class);
                     sp1.setIndicator("TAB1").setContent(intent1);
                     tabHost.setCurrentTab(0);
                     }
                  }
            });

            Button addNewButton = (Button)findViewById(R.id.add_new_ticket_btn);
            addNewButton.setOnClickListener(new OnClickListener(){
                  @Override
                  public void onClick(View v) {
                     Intent in = new Intent().setClass(getApplicationContext(), AddNewTicketActivity.class);
                     sp1.setContent(in);
                     tabHost.setCurrentTab(0);
                     //startActivity(in);
                 }
            });               
      }
}
Hiral Vadodaria
  • 19,158
  • 5
  • 39
  • 56
Romi
  • 4,833
  • 28
  • 81
  • 113

1 Answers1

1

As I understand, when you click the button AddNewTicketActivity is shown in TAB1. Then you go to TAB2 and when you come back to TAB1, you want to see TAB1Actvity. In that case try this in onTabChanged method

          if (tabId.equals("TAB2")){
                    Intent intent1 = new Intent(this, Tab1Activity.class);
                    sp1.setIndicator("TAB1").setContent(intent1);
           }
user994886
  • 444
  • 4
  • 13
  • yeah it worked, but am able to click on addNewTicket button only when i am on tab2, and after clicking on this button i came to tab1, whereas i want add button to click on each tab. – Romi Nov 24 '11 at 11:29
  • if you want the button to load AddNewTicket in every tab then modify the code as below Intent in = new Intent().setClass(getApplicationContext(), AddNewTicketActivity.class); sp1.setContent(in); tabHost.setCurrentTab(0); – user994886 Nov 24 '11 at 15:19
  • 1
    Disregard the previous comment if you want the button to load AddNewTicket in the current tab then try this in your button click listener - tabHost,setCurrent(TabHost.getCurrentTab()) – user994886 Nov 24 '11 at 15:31
  • user994886: Thamks alot :), need a bit more help. now am able to click on add button on each tab. but as clicking on add button, activate tab0, now i want that if i click on tab0 agn, tab0 activity should be displayed. – Romi Nov 25 '11 at 06:39