zoukankan      html  css  js  c++  java
  • Android GroupActivity 管理子activity跳转和回退

    public class ModelGroupTab extends ActivityGroup {
    
        LinearLayout container;
        //用于管理本Group中的所有Activity       
        public static ActivityGroup group;  
        @Override  
        protected void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
          //  setContentView(R.layout.modeltabgroup);
           // container=(LinearLayout)findViewById(R.id.modeltabgroup);
            group = this;  
        }  
      
        @Override  
        public void onBackPressed() {         
            //把后退事件交给子Activity处理  
            group.getLocalActivityManager().getCurrentActivity().onBackPressed();  
        }  
      
        @Override  
        protected void onStart() {  
            super.onStart();  
            //要跳转的Activity  
            Intent intent = new Intent(this, A_Activity.class);  
            // 把Activity转换成一个Window,然后转换成View  
            Window w = group.getLocalActivityManager().startActivity(  
                    "FirstActivity", intent);  
            View view = w.getDecorView();  
            //container.removeAllViews();
            //container.addView(view);
            //设置要跳转的Activity显示为本ActivityGroup的内容  
            group.setContentView(view);  
        }  
    }

    把按返回按钮的事件交给子activity处理

    初始化要跳转到的activity

    LocalActivityManager通过startActivity(String id, Intent intent),可以与指定的Actiivty绑定,

    @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                   Intent intent = new Intent(A_Activity.this, B_Activity.class);  
                   // 把Activity转换成一个Window,然后转换成View  
                   Window w = ModelGroupTab.group.getLocalActivityManage().startActivity("BActivity",intent);  
                   View view = w.getDecorView();
                   LinearLayout container=(LinearLayout) ModelGroupTab.group.getWindow().findViewById(R.id.modeltabgroup);
                
                   //设置要跳转的Activity显示为本ActivityGroup的内容  
                   ModelGroupTab.group.setContentView(view);  
            }

    Aactivity跳转到Bactivity

    @Override
        public void onBackPressed() {
            // TODO Auto-generated method stub
            //super.onBackPressed();
            
              Intent intent = new Intent(B_Activity.this, A_Activity.class);  
             
              // 把Activity转换成一个Window,然后转换成View  
              
              String id=ModelGroupTab.group.getLocalActivityManager() .getCurrentId();
    
              Window w = ModelGroupTab.group.getLocalActivityManager()  
                      .startActivity("A_Activity",intent);  
              View view = w.getDecorView();  
              
                        //设置要跳转的Activity显示为本ActivityGroup的内容  
              ModelGroupTab.group.setContentView(view);  
              System.out.println("activity "+id+" finsih");
              ModelGroupTab.group.getLocalActivityManager().destroyActivity(id, true);
    
             
        }

    Bactivity回退到Aactivity,覆写

    public void onBackPressed() 

    写完设置跳转的代码后,finish当前activity,先获得当前绑定的activity id,然后通过

    ModelGroupTab.group.getLocalActivityManager().destroyActivity(id, true);销毁

  • 相关阅读:
    Java实现直接插入查找
    CSS hack常用方案(摘选)
    输入一个字符串,去掉重复的字符,并按ASCII值排序-华为机试
    git操作github
    简单记录下idea上配置spring
    mysql国内镜像下载网址
    tomcat启动不成功(点击startup.bat闪退)的解决办法
    Encode and Decode TinyURL
    leetcode 4Sum II
    leetcode two sum
  • 原文地址:https://www.cnblogs.com/cschen/p/3381352.html
Copyright © 2011-2022 走看看