我在应用程序中使用FragmentActivities,需要用嵌套的Activities实现TabHost,这些Activities可以在彼此之间切换。我将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;
}
}
我切换另一个碎片活动,如下所示:
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);
另一个"活动"成功切换,但随后立即切换到初始状态。原因是什么?
我使用本教程解决了我的问题