在我的应用程序中,我尝试使用intent从选项卡中的按钮打开一个活动。但是,不是在同一个选项卡中打开,新活动将覆盖整个屏幕,并且选项卡不会延迟显示。如何在同一个选项卡中打开多个活动..
以下代码属于我的主要课程:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("OPT").setContent(new Intent(this, TabGroup1Activity.class)));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("EDIT").setContent(new Intent(this, TabGroup2Activity.class)));
tabHost.setCurrentTab(1);
}
以下是TabGroupActivity的代码:
public class TabGroupActivity extends ActivityGroup{
ArrayList<String> list;
Window window;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
if(list.equals(null))
{
list = new ArrayList<String>();
}
}
@Override
public void finishFromChild(Activity child)
{
LocalActivityManager manager = getLocalActivityManager();
int index = list.size() -1;
if(index < 1)
{
finish();
return;
}
manager.destroyActivity(list.get(index), true);
list.remove(index);
index--;
String lastId = list.get(index);
Intent in = manager.getActivity(lastId).getIntent();
window = manager.startActivity(lastId, in);
setContentView(window.getDecorView());
}
public void startChildActivity(String Id, Intent intent)
{
window = getLocalActivityManager().startActivity(Id, intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
if(window != null)
{
list.add(Id);
setContentView(window.getDecorView());
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
return true;
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
{
onBackPressed();
return true;
}
return super.onKeyUp(keyCode, event);
}
@Override
public void onBackPressed()
{
int length = list.size();
if(length > 1)
{
Activity current = getLocalActivityManager().getActivity(list.get(length - 1));
current.finish();
}
}
}
TabGroup1Activity的代码TabGroup2Activity也具有相同的代码:
public class TabGroup1Activity extends TabGroupActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startChildActivity("TestClass", new Intent(this,TestClass.class));
}
}
TestClass活动的代码:
public class TestClass extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.newview);
}
}
请帮助我解决我的问题..
提前致谢...
答案 0 :(得分:1)