Search code examples
androidandroid-activityandroid-tabs

Android: Remove history of child activities in tab group


I have a tab group with 4 tabs. In 2nd tab i have 4 child activities, after going from child 1 => child 2 => child 3 => child 4 and when i click a button it should return to child 1 as a fresh activity without having the history of child 2 3 4.

i have tried this: starting child 1 from child 4,

TabGroupActivity parentActivity = (TabGroupActivity) getParent();

    Intent intent = new Intent(getParent(),child1.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    parentActivity.startChildActivity("child", intent);

can anyone help me.


Solution

  • Start your "child" activity with just startActivity, then when back pressed on the child activity, capture onBackPressed on the child activity and start your parent activity again with a new intent like so:

    @Override
    public void onBackPressed() {
       startActivity(new Intent(this, FirstActivity.class)
         .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK));
    }
    

    This should start you fresh with a newly started parent activity.