Android Tab view

旧街凉风 提交于 2019-12-03 08:40:34

Ok i am providing a demo i hope it will help you ....

Firs of all declare one ActivityGroup like this SalesActivityGroup.java

public class SalesActivityGroup extends ActivityGroup {  

                // Keep this in a static variable to make it accessible for all the nested activities, lets them manipulate the view  
            public static SalesActivityGroup group;  

                // Need to keep track of the history if you want the back-button to work properly, don't use this if your activities requires a lot of memory.  
           private ArrayList<View> history;  

            @Override  
            protected void onCreate(Bundle savedInstanceState) {  
                  super.onCreate(savedInstanceState);  
                  this.history = new ArrayList<View>();  
                  group = this;  

                      // Start the root activity withing the group and get its view  
                  View view = getLocalActivityManager().startActivity("Home", new  
                                                    Intent(this,SalesRouteActivity.class)  
                                                    .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
                                                    .getDecorView();  

                      // Replace the view of this ActivityGroup  
                  replaceView(view);  

               }  

            public void replaceView(View v) {  
                        // Adds the old one to history  
                history.add(v);  
                        // Changes this Groups View to the new View.  
                setContentView(v);  

            }  

            public void back() {  
                if(history.size() > 0) {  
                    history.remove(history.size()-1);  
                    if(history.size() > 0) {
                         setContentView(history.get(history.size()-1));  
                    }
                    else {  
                        finish();  
                    } 
                }else {  
                    finish();  
                }  
            }  

           @Override  
            public void onBackPressed() {  
                SalesActivityGroup.group.back();  
                return;  
            }  

  }  

After this change your host(mainActivity)(Change only one TabSpec : firstTabSpec which is related to sales i guess) like this ...

public class Host extends TabActivity {

    public static Button btnRed;
    public static TabHost tabHost; 

    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.host);

            tabHost = (TabHost)findViewById(android.R.id.tabhost);


            TabSpec salesTabSpec = tabHost.newTabSpec("tid1");

            Intent intent1 = new Intent(this, SalesActivityGroup.class);//SalesActivityGroup instead of SalesRouteActivity

            salesTabSpec.setContent(intent2);

             /* Add tabSpec to the TabHost to display. */
            tabHost.addTab(salesTabSpec);

        }
}

Afterward when ever you want to start new Activity in firstTab(salesTab) you just need to change view of ActivityGroup related to that salesTab

like this (start your listRetailerActivity following way )...

Intent intent = new Intent(SalesRouteActivity.this, ListRetailerActivity.class);

            // Create the view using FirstGroup's LocalActivityManager  
            View view = SalesActivitytGroup.group.getLocalActivityManager()  
            .startActivity("", intent  
                    .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
                    .getDecorView();  

            // Again, replace the view  
            SalesActivityGroup.group.replaceView(view);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!