我在我的应用程序中使用FragmentActivities,需要使用嵌套的活动实现TabHost,这些活动可以相互切换。我为此目的使用ActivityGroup:
public class CustomActivityGroup extends ActivityGroup {
View rootView;
public static CustomActivityGroup group;
private ArrayList<View> history;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.history = new ArrayList<View>();
group = this;
View view = getLocalActivityManager().startActivity("RegisterActivity", new Intent(this,RegisterActivity.class))
.getDecorView();
replaceView(view);
}
public void replaceView(View v) {
history.add(v);
setContentView(v);
}
public void back() {
try {
if (history.size() > 0) {
history.remove(history.size() - 1);
if (history.size() > 0) {
setContentView(history.get(history.size() - 1));
} else {
finish();
}
} else {
finish();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public int getHistorySize() {
return history.size();
}
@Override
public void onBackPressed() {
try {
CustomActivityGroup.group.back();
} catch (Exception ex) {
}
return;
}
}
我像这样切换另一个FragmentActivity:
Intent i = new Intent(this, RegisterActivityCompl.class);
View view = CustomActivityGroup.group.getLocalActivityManager()
.startActivity("RegisterActivityCompl", i
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
// Again, replace the view
CustomActivityGroup.group.replaceView(view);
另一个Activity成功切换,但随后立即切换到初始状态。可能是什么原因?