Search code examples
androidandroid-activityandroid-tabhost

Android TabActivity: How to start only the activity from the currenttab?


In my activity I have a TabHost.

I have 3 tabs and 3 activities for them. How can I start the corresponding activity when I click on a tab?

At the moment all three activities starts...

If I run this code, every activity (connected_upload, connected_download, connected_search) runs the "onCreate" method.

How can I start those activities manually? I mean I like to start the activity only when I click on the corresponding tab...

public class connected extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.connected);

        Resources res = getResources(); // Resource object to get Drawables   
        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, connected_upload.class);      
        spec = tabHost.newTabSpec("Hoch").setIndicator("Hoch",res.getDrawable(R.drawable.freeftp)).setContent(intent); 
        tabHost.addTab(spec);   

        intent = new Intent().setClass(this, connected_download.class); 
        String str_path = getIntent().getStringExtra("path");
        String str_profil = getIntent().getStringExtra("profil");
        String str_server = getIntent().getStringExtra("server");
        String str_port = getIntent().getStringExtra("port");
        String str_user = getIntent().getStringExtra("user");
        String str_password = getIntent().getStringExtra("pw");

        intent.putExtra("path", str_path);
        intent.putExtra("profil", str_profil);
        intent.putExtra("server", str_server);
        intent.putExtra("port", str_port);
        intent.putExtra("user", str_user);
        intent.putExtra("pw", str_password);

        spec = tabHost.newTabSpec("Herunter").setIndicator("Herunter",res.getDrawable(R.drawable.freeftp)).setContent(intent); 
        tabHost.addTab(spec); 

        intent = new Intent().setClass(this, connected_search.class); 
        spec = tabHost.newTabSpec("Search").setIndicator("Search",res.getDrawable(R.drawable.freeftp)).setContent(intent); 
        tabHost.addTab(spec);

        tabHost.setCurrentTab(1);

        //Button Connect Server
        Button cmd_mainsite = (Button)findViewById(R.id.but_connected_mainsite);
        cmd_mainsite.setOnClickListener(new View.OnClickListener(){         
            public void onClick(View v){
                finish();
                Intent Intent_mainsite = new Intent(connected.this, Login.class);
                startActivity(Intent_mainsite); 
            }       
        });

    }
}

Solution

  • Use This Code As per your requirement

    MainActivity.java

    public class MainActivity extends TabActivity {
    
    
                            @Override
                            public void onCreate(Bundle savedInstanceState) {
                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.main);
    
                               Resources res = getResources();
                                TabHost th = getTabHost();
    
                                th.addTab(th.newTabSpec("").setIndicator("tab1",
                                        res.getDrawable(R.drawable.icon)).
                                        setContent(new Intent(this, firsttab.class)));
                                th.addTab(th.newTabSpec("").setIndicator("tab2")
                                        .setContent(new Intent(this, secondtab.class)));
                                th.addTab(th.newTabSpec("").setIndicator("tab3")
                                        .setContent(new Intent(this, thirdtab.class)));
    
        th.setCurrentTab(1);
                    }
                    }
    

    main.xml

    <?xml version="1.0" encoding="utf-8"?>
    
        <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@android:id/tabhost" 
            android:layout_width="match_parent"
            android:layout_height="match_parent">
                <LinearLayout 
                android:layout_width="match_parent" 
                android:id="@+id/linearLayout1" 
                android:layout_height="match_parent" 
                android:orientation="vertical">
    
                    <FrameLayout 
                    android:layout_width="match_parent"
                     android:layout_height="match_parent" 
                     android:id="@android:id/tabcontent"
                     android:layout_weight="1">
    
                    </FrameLayout>
                     <TabWidget 
                    android:layout_width="match_parent" 
                    android:layout_height="wrap_content" 
                    android:id="@android:id/tabs">
                    </TabWidget>
                </LinearLayout>
            </TabHost>
    

    firsttab.java

    public class firsttab extends Activity {
    
            /** Called when the activity is first created. */
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
    
                // TODO Auto-generated method stub
                TextView tv = new TextView(this);
                tv.setText("HI");
                tv.setTextSize(25);
                setContentView(tv);
            }
    
        }
    

    secondtab.java

    public class secondtab extends Activity {
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            // TODO Auto-generated method stub
            TextView tv = new TextView(this);
            tv.setText("Hello");
            tv.setTextSize(25);
            setContentView(tv);
        }
    
    }
    

    thirdtab.java

    public class thirdtab extends Activity {
    
            /** Called when the activity is first created. */
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
    
                // TODO Auto-generated method stub
                TextView tv = new TextView(this);
                tv.setText("How Are U?");
                tv.setTextSize(25);
                setContentView(tv);
            }
    
        }