1

I have activities with enter and exit transitions
Depends on actvity called to start, I have different shared elements
As you know I should use below method as secound parameter when calling startActivity():

ActivityOptions.makeSceneTransitionAnimation(Activity activity,
                                                       Pair<View, String>... sharedElements);

Because I use my different shared elements several times in code, I want to store my pairs in arrays to use for varags parameter.
By using this:

Pair<View, String>[] pairs = new Pair<View, String>[n];

I get "Generic array creation" error.

I have also tried using Arraylist like this:

ArrayList<Pair<View, String>> p = new ArrayList<>();
p.add(Pair.create(pauseButton, "sharedPauseBtn"));
p.add(Pair.create(toggleMusicButton, "sharedMusicBtn"));
Pair<View, String>[] pairs = p.toArray(new Pair<View, String>[n]);

And getting same "Generic array creation" error, I think Java doesn't allow it.

Any idea for storing some "Pair<View, String>" in an array as shared elements or another alternative solutions? I have searched all questions about array of generic class object in Java in stackoverflow but they doesn't help.

1 Answers1

0

To make it using Kotlin syntax without arrays:

val firstTransition = Pair.create<View, String>(view1, "transitionName1")
val secondTransition = Pair.create<View, String>(view2, "transitionName2")
val options = ActivityOptions.makeSceneTransitionAnimation(this@CurrentActivity, firstTransition, secondTransition)

val i = Intent(this@CurrentActivity, TargetActivity::class.java)
startActivity(i, options.toBundle())

To make an array of Pair objects in your activity just use the following Java syntax:

Pair[] pairs = new Pair[2];
pairs[0] = Pair.create(view1, "transitionName1");
pairs[1] = Pair.create(view2, "transitionName2");

ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(this,pairs);

Intent i = new Intent(CurrentActivity.this, TargetActivity.class);
startActivity(i, options.toBundle());
Ahmed Maad
  • 367
  • 5
  • 16