我在我的应用程序中使用 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 切换成功,但随后立即切换到初始状态。这可能是什么原因?