Android TaskStackBuilder ugly transition

295 Views Asked by At

What on earth is wrong with TaskStackBuilder that it uses this ugly transition when starting new activities.:

    TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(this)
            .addParentStack(ActivityB.class)
            .addNextIntent(new Intent(this, ActivityB.class));
    taskStackBuilder.startActivities();

This is basically standard code that google however if you run this code you will see a super ugly transition when going to ActivityB.

I guess its because its a new Task. But I dont want it to look like this is there anything that I can do ?

Thanks !

1

There are 1 best solutions below

0
On

After digging inside TaskStackBuilder's implementation, the problem is that it forces adding Intent.FLAG_ACTIVITY_CLEAR_TASK to the 1st intent in the stack, which makes that ugly transition, so use the following to start the stack:

Intent[] intents = TaskStackBuilder.create(this)
            .addParentStack(ActivityB.class)
            .addNextIntent(new Intent(this, ActivityB.class))
            .getIntents();
if (intents.length > 0) {
    intents[0].setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);// Or any other flags you want, but not the `.._CLEAR_..` one
}
// `this` inside current activity, or you can use App's context
this.startActivities(intents, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());

The idea here is to still use the TaskStackBuilder for creating your intents' stack, then remove the weird Intent.FLAG_ACTIVITY_CLEAR_TASK that the TaskStackBuilder adds to the 1st intent, then start the activities manually using any Context you want.