Используйте класс ActivityGroup с ViewAnimator и получите правильную активность по ID.
public class YourActivity extends ActivityGroup {
private Stack<String> ids;
private LocalActivityManager activityManager;
private ViewAnimator animator;
private int serial;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.group);
ids = new Stack<String>();
animator = (ViewAnimator) findViewById(R.id.animator);
activityManager = getLocalActivityManager();
}
@Override
public void startActivity(final Intent intent) {
String id = "id" + serial++;
ids.push(id);
View view = activityManager.startActivity(id, intent).getDecorView();
animator.addView(view);
animator.setDisplayedChild(ids.size() - 1);
}
@Override
public boolean onKeyDown(final int keyCode, final KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
int size = ids.size();
if (size > 0) {
String topId = ids.pop();
View view = activityManager.destroyActivity(topId, true).getDecorView();
animator.removeView(view);
if (size > 1) {
topId = ids.get(size - 2);
if (activityManager.getActivity(topId) instanceof ClassA) {
((ClassA) activityManager.getActivity(topId)).onResume();
} else if (activityManager.getActivity(topId) instanceof ClassB) {
((ClassB) activityManager.getActivity(topId)).onResume();
}
return true;
}
}
}
return super.onKeyDown(keyCode, event);
}
}