TaskStackBuilder transition animation

AndroidAnimationAndroid IntentTaskstackbuilder

Android Problem Overview


I'm using Android L transitions passing an ActivityOptions bundle in intent. How can I reproduce the animation on the same intent with TaskStackBuilder?

This is my current working method with a single Intent:

startActivity(myIntent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());

This is my try with TaskStackBuilder:

 TaskStackBuilder builder = TaskStackBuilder.create(this);
 builder.addNextIntentWithParentStack(myIntent);
 builder.startActivities(ActivityOptions.makeSceneTransitionAnimation(this).toBundle());

But the animation creates a strange effect, not the same one of the "single-intent" version.

I also tried with:

builder.addNextIntent(myIntent);

instead of:

builder.addNextIntentWithParentStack(myIntent);

Android Solutions


Solution 1 - Android

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 strange effect, so use the following to start the stack:

Intent[] intents = TaskStackBuilder.create(this)
                     .addNextIntentWithParentStack(myIntent)
                     .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.

Solution 2 - Android

Try to do it using:

TaskStackBuilder.create (Context context)

Return a new TaskStackBuilder for launching a fresh taskstack consisting of a series of activities. Parameters -

Context context: The context that will launch the new task stack or generate a PendingIntent.

Returns, TaskStackBuilder - a new TaskStackBuilder.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGiorgio AntonioliView Question on Stackoverflow
Solution 1 - AndroidAbdelHadyView Answer on Stackoverflow
Solution 2 - AndroidRadFoxView Answer on Stackoverflow