动态选择Tab Activity,以便在用户单击选项卡时启动

时间:2011-06-10 19:26:27

标签: android android-activity android-tabhost activitygroup

当您在选项卡中使用嵌套活动时,我看到很多关于使用ActivityGroups的帖子。就我而言,我不想在活动中启动活动,而是希望动态决定(基于标记值)每次用户点击选项卡时要启动的活动。

我正在尝试这样的事情:

标签活动类:

 tabHost = (TabHost) findViewById(android.R.id.tabhost); 
 tabHost.setup();
 intent = new Intent().setClass(this, MyActivityGroup.class);
 TabHost.TabSpec spec = tabHost.newTabSpec("someTag");      
 View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_resource, getTabWidget(), false);
 TextView title = (TextView) tabIndicator.findViewById(R.id.tab_title); 
 title.setText(strTag);
 ImageView icon = (ImageView) tabIndicator.findViewById(R.id.tab_icon);
 icon.setImageResource(R.drawable.tab_icon);
 spec.setIndicator(tabIndicator).setContent(intent); 
 tabHost.addTab(spec);

MyActivityGroup类:

 public class MyActivityGroup extends ActivityGroup
 {
    public static MyActivityGroup group;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {  
       group = this;  
           SelectActivity();
   }  

      public void SelectActivity()
      {
        View view = null;
        Intent intent = null;
       Window w = null;
       LocalActivityManager l = getLocalActivityManager();
       if( CheckFlag() )
       {
           intent = new Intent(this, Activity1.class);
           w = l.startActivity("Activity 1", intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
           view = w.getDecorView();  
       }
       else
       {
       intent = new Intent(this,Activity2.class);
       w = l.startActivity("Activity 2", intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
           view = w.getDecorView();  
  }
  setContentView(view);  
  }

}

我将Activity1,Activity2和MyActivityGroup添加到我的清单文件中。如果在我的选项卡类中,当我设置选项卡的意图时,我将MyActivityGroup替换为Activity1或Activity2,那么它们就可以正常启动。但是,在调用startActivity时,使用MyActivityGroup会导致MyActivityGroup :: SelectActivity崩溃。

有什么建议吗? :s

1 个答案:

答案 0 :(得分:0)

原来我不得不打电话

1 .dispatchCreate(savedInstanceState);

SelectActivity()之前

。希望这有助于某人!