1

I created an activity ( activity no. 1 ) with 2 Views, one button and one textView these two views are shared to another activity ( activity no. 2 )

I use the following code to start from activity1 to activity2 with the shared elements:

Pair textView = new Pair<>(view1, ViewCompat.getTransitionName(view1)); Pair button = new Pair<>(view2, ViewCompat.getTransitionName(view2)); ActivityOptionsCompat transitionActivityOptions = ActivityOptionsCompat.makeSceneTransitionAnimation( Activity1.this, textView, button); Intent intent = new Intent(Activity1.this, Activity2.class); ActivityCompat.startActivity(Activity1.this, intent, transitionActivityOptions.toBundle()); 

When I press the back button the views are animated back to Activity1.The animation stop from work after I Override the back buton with the following code:

@Override public void onBackPressed(){ Intent intent = new Intent(Detailed.this, Main.class); intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); intent.putExtra("morph", morphButton.getMorph()); startActivity(intent); finish(); } 

The startActivity method cancel the animation.

I am trying to achieve this because I want to pass back to Activity1 some variables. Is there a way how can I maintain the animation with startActivity method ?

1
  • you can use 'setCustomAnimations(R.anim.enter, R.anim.exit, R.anim.pop_enter, R.anim.pop_exit);' method as per your needs after startActivity to animate the activites Commented Jan 10, 2017 at 11:48

1 Answer 1

2

That's because you're starting a new activity in your onBackPressed instead of just calling finish();. You can use startActivityResult instead and set a result in your Activity1.
In your Activity1 use this instead:

ActivityCompat.startActivityForResult(Activity1.this, intent, yourReqCode, yourRetransitionActivityOptions.toBundle()); 

Then in Your onBackPressed set the result:

@Override public void onBackPressed(){ Intent intent = new Intent(); intent.putExtra("morph", morphButton.getMorph()); setResult(RESULT_OK, intent); finish(); } 

Then fish your results in onActivityResult of Activity1:

 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case yourReqCode: if (resultCode == RESULT_OK) { // fetch the result from data here } break; } super.onActivityResult(requestCode, resultCode, data); } 
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.