问题
In my application I am trying to open an activity from a button in tab using intent. But instead of opening in the same tab the new activity covers the entire screen and the tabs are no linger visible.. How can I open more than one activity in the same tab..
The following code is of my main class:
@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);
}
The following is the code of 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();
}
}
}
The code for TabGroup1Activity the TabGroup2Activity also having the same code:
public class TabGroup1Activity extends TabGroupActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startChildActivity("TestClass", new Intent(this,TestClass.class));
}
}
Code for the TestClass activity:
public class TestClass extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.newview);
}
}
Please help me is solving my problem..
Thanks in advance...
回答1:
You can do it using fragments..check http://mobile.tutsplus.com/tutorials/android/android-compatibility-working-with-fragments/
来源:https://stackoverflow.com/questions/12258720/multiple-activities-in-single-tab-android