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
  • which line causes the error? Commented Jan 5, 2021 at 16:22

1 Answer 1

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()); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.