user1320395
user1320395

Reputation: 21

how to start new activity from list activity within tabview

I am developing new application where I am using tab view as parent layout. I'm using TabHost to display 3 tabs within my application. Each of these tab has separate Activity containing a ListView. This is working fine. When you click on an item within the ListView it currently loads up a brand new Activity full screen leaving the TabHost. I'd like to load up these Activities within the TabHost. I want to retain the tabview after calling another activities from list view.

Thank you both for your response. Here is my code where I need your help.

################HelloTabWidget

//This class displays the tab view with 3 tab - Customer, Company and City.

    public class HelloTabWidget extends TabActivity {
    //public class HelloTabWidget extends ActivityGroup {
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            Resources res = getResources();


        TabHost tabHost = getTabHost(); 
        TabHost.TabSpec spec; 
        Intent intent; 
        intent = new Intent().setClass(this, CustomerTabView.class);
        spec = tabHost
                .newTabSpec("Customer")
                .setIndicator("Customer",
                        res.getDrawable(R.drawable.ic_tab_Customer))
                .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, CompanyTabView.class);
        spec = tabHost
                .newTabSpec("Company")
                .setIndicator("Company",
                        res.getDrawable(R.drawable.ic_tab_Company))
                .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, CityTabView.class);
        spec = tabHost
                .newTabSpec("City")
                .setIndicator("City", res.getDrawable(R.drawable.ic_tab_City))
                .setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);
    }
}
################CustomerTabView

//This class displays list view of customers names. On click on any item in the list, it should open customer detail page keeping same tabs view.

public class CustomerTabView extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String[] category = getResources().getStringArray(
                R.array.category_array);
        setListAdapter(new ArrayAdapter<String>(this, R.drawable.list_items,
                category));
        ListView lv = getListView();
        lv.setTextFilterEnabled(true);

        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                //Need this logic where I can retain the tab view and call new activity class for customerdetails view.                 

                Intent intent; 
                intent = new Intent(CustomerTabView.this,
                        C_DetailActivity.class);
                startActivity(intent);
                finish();
            }

        });
    }
}
################C_DetailActivity

On click of any item from customertabview, this activity class gets call which shows details of the customer.

public class C_DetailActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        TextView textview = new TextView(this);
        textview.setText("This is the Customer Details view");
        setContentView(textview);
    }
}

After calling C_DetailActivity class, tab view disappear. I want to retain the main tab view. So need this logic where I can retain the tab view and call new activity class for customerdetails view

Upvotes: 2

Views: 1689

Answers (1)

Wietse de Vries
Wietse de Vries

Reputation: 685

ListView lv = (ListView) findViewById(R.id.myListView);
lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView parent, View v, int position, long id) {
        LocalActivityManager manager = getLocalActivityManager();
        String currentTag = tabHost.getCurrentTabTag();
        manager.destroyActivity(currentTag, true);
        manager.startActivity(currentTag, new Intent(this, newClass.class));
    }
});

Not tested, but something like this should work. If is doesn't I'll help you to fix it.

Upvotes: 1

Related Questions