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 an array of Pair objects in your activity just use the following syntax:

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

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


    Intent i = new Intent(this, YourActivity.class);
    startActivity(i, options.toBundle());
Ahmed Maad
  • 127
  • 10