Question

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. i.e tab0 is active on tab0 event and on button click event. And am able to change the tab events using tabHost.setOnTabChangedListener, but now what i further want is, say suppose i click on button so now button view is displaying but again if i click on tab0, tab0 activity should be displayed.

i used onClick but after using this my TabChangeListner not works.

would you suggest solution for my problem.

Thanks.

Below is my code that is working fine :

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("TAB2")){
                     Log.i("TAB1", "TAB1 Changed");
                     Intent intent1 = new Intent().setClass(getApplicationContext(), Tab1Activity.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);
                 }
            });               
      }
}
Was it helpful?

Solution

You need to use AvtivityGroup to open new activities in the same tab.

Here is the complete example.

http://ericharlow.blogspot.com/2010/09/experience-multiple-android-activities.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top